C
C#2y ago
morry329#

❔ 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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Moods
Moods2y ago
ooo, dynamic programming
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Moods
Moods2y ago
yeah i was going to say it just returns immediately doesnt even recurse or anything
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Moods
Moods2y ago
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
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
morry329#
morry329#OP2y ago
Ahhh ok thank you all for looking it up 🙏
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server