Skip to content

Commit b83575f

Browse files
authored
Update Solution.cpp
1 parent 024f357 commit b83575f

File tree

1 file changed

+13
-6
lines changed
  • solution/0700-0799/0746.Min Cost Climbing Stairs

1 file changed

+13
-6
lines changed

solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ class Solution {
22
public:
33
int minCostClimbingStairs(vector<int>& cost) {
44
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];
5+
int f[n];
6+
memset(f, -1, sizeof(f));
7+
auto dfs = [&](auto&& dfs, int i) -> int {
8+
if (i >= n) {
9+
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));
1017
}
11-
};
18+
};

0 commit comments

Comments
 (0)