https://leetcode.com/problems/min-cost-climbing-stairs/
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
// 10, 15, 20, 0
// 0, 1, 2, 3
cost.push_back(0);
for(int i = cost.size() - 3; i >= 0; i--) {
cost[i] += min(cost[i+1], cost[i+2]);
}
return min(cost[0], cost[1]);
}
};
best
class Solution {
public:
int mini(int a,int b)
{
return a>b?b:a;
}
int minCostClimbingStairs(vector<int>& cost) {
int i,n;
n=cost.size();
if(n==1)return cost[0];
int min;
for(i=2;i<n;i++)
{
min=mini(cost[i-1],cost[i-2]);
cost[i]+=min;
}
return mini(cost[n-2],cost[n-1]);
}
};
References
https://www.youtube.com/watch?v=ktmzAZWkEZ0
728x90
반응형
'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글
1148. Article Views I (0) | 2022.06.03 |
---|---|
1581. Customer Who Visited but Did Not Make Any Transactions (0) | 2022.06.03 |
70. Climbing Stairs (0) | 2022.05.31 |
176. Second Highest Salary (0) | 2022.05.30 |
608. Tree Node (0) | 2022.05.30 |