맨 아래 칸부터 하나 혹은 두 칸씩 올라가면서 최소 칸 수를 메모하며 올라간다.
class Solution {
public int minCostClimbingStairs(int[] cost) {
int start = 0;
int top = cost.length + 2;
int[] m_cost = new int[top];
for(int i = 0; i < cost.length; i++) {
m_cost[i + 1] = cost[i];
}
int[] minimum = new int[top];
Arrays.fill(minimum, Integer.MAX_VALUE);
minimum[0] = 0;
// Time O(n) / Space O(n)
while(start < top) {
if (start + 1 < top && minimum[start + 1] > m_cost[start + 1] + minimum[start]) {
minimum[start + 1] = m_cost[start + 1] + minimum[start];
}
if (start + 2 < top && minimum[start + 2] > m_cost[start + 2] + minimum[start]) {
minimum[start + 2] = m_cost[start + 2] + minimum[start];
}
start += 1;
}
return minimum[top - 1];
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 136. Single Number (0) | 2024.06.05 |
---|---|
[leetcode] 338. Counting Bits (0) | 2024.06.03 |
[leetcode] 1137. N-th Tribonacci Number, 62. Unique Paths (0) | 2024.05.31 |
[leetcode] 374. Guess Number Higher or Lower (0) | 2024.05.31 |
[leetcode] 17. Letter Combinations of a Phone Number (0) | 2024.05.28 |