๋์ด๋: Easy
ํค์๋: DP
๐ ๋ฌธ์
์ ์ ๋ฐฐ์ด cost๊ฐ ์ฃผ์ด์ง๋ฉฐ, cost[i]๋ ๊ณ๋จ์ i๋ฒ์งธ ๋จ๊ณ์ ๋น์ฉ์
๋๋ค. ๋น์ฉ์ ์ง๋ถํ๋ฉด 1๊ฐ ๋๋ 2๊ฐ์ ๋จ๊ณ๋ฅผ ์ค๋ฅผ ์ ์์ต๋๋ค.
์ธ๋ฑ์ค 0 ๋๋ ์ธ๋ฑ์ค 1์ ๋จ๊ณ์์ ์์ํ ์ ์์ต๋๋ค.
์ต์์ ์ธต์ ๋๋ฌํ๊ธฐ ์ํ ์ต์ ๋น์ฉ์ ๋ฐํํ์ธ์.
Example 1:
Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.
๋ฌธ์ ์๋ฌธ: https://leetcode.com/problems/min-cost-climbing-stairs/description/
๐ ๋ฌธ์ ํ์ด
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
const int N = cost.size();
vector<int> a(N+1, 0);
if(N <= 1) return 0;
a[0] = 0;
a[1] = 0;
for(int n=2; n <= N; ++n){
a[n] = min(a[n-1] + cost[n-1], a[n-2] + cost[n-2]);
}
return a[N];
}
};
๋ฐ์ํ
'IN DEPTH CAKE > Supercoder' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++/LeetCode-DP] 91. Decode Ways (w/ DP ํ์ด ์ ๋ต) (3) | 2024.05.06 |
---|---|
[c++/LeetCode-DP] 70. Climbing Stairs (1) | 2024.05.06 |
[c++/LeetCode-Hash Table] 939. Minimum Area Rectangle (1) | 2024.05.05 |
[c++/LeetCode-Hash Table] 692. Top K Frequent Words (1) | 2024.05.05 |
[c++/LeetCode-Hash Table] 387. First Unique Character in a String (1) | 2024.05.05 |