Skip to content

Commit

Permalink
solutions: 0746 - Min Cost Climbing Stairs (Easy)
Browse files Browse the repository at this point in the history
- add c++ solution
- add kotlin solution
  • Loading branch information
wingkwong committed Oct 13, 2023
1 parent fb80b10 commit 58dc680
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion solutions/0700-0799/0746-min-cost-climbing-stairs-easy.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: 'Author: @ColeB2 | https://leetcode.com/problems/min-cost-climbing-stairs/'
description: 'Author: @ColeB2, @wingkwong | https://leetcode.com/problems/min-cost-climbing-stairs/'
tags: [Array, Dynamic Programming]
---

Expand Down Expand Up @@ -77,5 +77,47 @@ class Solution:
return min(cost[-1], cost[-2])
```

</TabItem>

<TabItem value="cpp" label="C++">
<SolutionAuthor name="@wingkwong"/>

```cpp
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
int dp[n + 1];
memset(dp, 0, sizeof(dp));
dp[0] = cost[0];
dp[1] = cost[1];
for (int i = 2; i <= n; i++) {
dp[i] = min(dp[i - 1], dp[i - 2]) + (i == n ? 0 : cost[i]);
}
return dp[n];
}
};
```
</TabItem>
<TabItem value="kotlin" label="Kotlin">
<SolutionAuthor name="@wingkwong"/>
```kt
class Solution {
fun minCostClimbingStairs(cost: IntArray): Int {
val n = cost.size
val dp = IntArray(n + 1)
dp[0] = cost[0]
dp[1] = cost[1]
for (i in 2 .. n) {
dp[i] = minOf(dp[i - 1], dp[i - 2]) + if (i == n) 0 else cost[i]
}
return dp[n]
}
}
```

</TabItem>
</Tabs>

0 comments on commit 58dc680

Please sign in to comment.