반응형
https://leetcode.com/problems/min-cost-climbing-stairs/
Min Cost Climbing Stairs - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
dp[i] = i번째에 도달할 때의 최소합을 저장해줍니다.
1칸 또는 2칸씩만 점프할 수 있으므로
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i] 이 됩니다.
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
for(int i=2; i<cost.size(); i++)
cost[i] = min({cost[i - 1], cost[i - 2]}) + cost[i];
return min(cost[cost.size() - 1], cost[cost.size() - 2]);
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 576 : Out of Boundary Paths (0) | 2022.07.16 |
---|---|
LeetCode 695 : Max Area of Island (0) | 2022.07.16 |
LeetCode 1696 : Jump Game VI (0) | 2022.07.09 |
LeetCode 1465 : Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (0) | 2022.07.02 |
LeetCode 1423 : Maximum Points You Can Obtain from Cards (0) | 2022.06.26 |