❔ Why wrong answer?
So I am trying to resolve this puzzle https://leetcode.com/problems/min-cost-climbing-stairs/?envType=study-plan&envId=level-1&plan=leetcode-75
My progress in the code is as follows:
public class Solution {
public int MinCostClimbingStairs(int[] cost) {
int i;
int[] dp = new int[cost.Length-1];
for(i = 0; i < cost.Length; i++){
if(i < 2){
dp[i] = cost[i];
return dp[i];
} else {
dp[i] += cost[i] + Math.Min(dp[i+1], dp[i+2]);
}
}
return Math.Min(dp[0], dp[1]);
}
}
And it still gets wrothe ng answer (see screenshot). Could anyone kindly point out to me what went wrong with the code?9 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
ooo, dynamic programming
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah i was going to say it just returns immediately
doesnt even recurse or anything
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
i can send you my solution for it (cus i forgot how i even did it)
actually
the issue with your code
remove the return statement after the conditional
no reason why it should be there
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Ahhh ok thank you all for looking it up 🙏
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.