We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 024f357 commit b83575fCopy full SHA for b83575f
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp
@@ -2,10 +2,17 @@ class Solution {
2
public:
3
int minCostClimbingStairs(vector<int>& cost) {
4
int n = cost.size();
5
- vector<int> f(n + 1);
6
- for (int i = 2; i <= n; ++i) {
7
- f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
8
- }
9
- return f[n];
+ int f[n];
+ memset(f, -1, sizeof(f));
+ auto dfs = [&](auto&& dfs, int i) -> int {
+ if (i >= n) {
+ return 0;
10
+ }
11
+ if (f[i] < 0) {
12
+ f[i] = cost[i] + min(dfs(dfs, i + 1), dfs(dfs, i + 2));
13
14
+ return f[i];
15
+ };
16
+ return min(dfs(dfs, 0), dfs(dfs, 1));
17
}
-};
18
+};
0 commit comments