Skip to content

Commit 70a497a

Browse files
Add files via upload
1 parent 14b8f65 commit 70a497a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 动态规划
2+
3+
// Runtime: 12 ms, faster than 14.19% of C++ online submissions for Min Cost Climbing Stairs.
4+
// Memory Usage: 8.6 MB, less than 87.09% of C++ online submissions for Min Cost Climbing Stairs.
5+
6+
class Solution
7+
{
8+
public:
9+
int minCostClimbingStairs(vector<int>& cost)
10+
{
11+
if (cost.size() <= 1)
12+
return 0;
13+
14+
vector<int> memo(cost.size());
15+
memo[0] = 0;
16+
memo[1] = min(cost[0], cost[1]);
17+
18+
for (int i = 2; i < cost.size(); ++i)
19+
{
20+
memo[i] = min(memo[i - 1] + cost[i], memo[i - 2] + cost[i - 1]);
21+
}
22+
return memo.back();
23+
}
24+
};

0 commit comments

Comments
 (0)