
문제
https://leetcode.com/problems/climbing-stairs/description/
입력
1 <= n <= 45
결과
ans: Int = n계단까지 올라갈 수 있는 경우의 수
해석
한번에 1칸 또는 2칸까지 올라갈 수 있으니 목적지(n) 기준 n-1과 n-2 경우의 수를 더하면 끝
코드
class Solution {
func climbStairs(_ n: Int) -> Int {
if n <= 2 {
return n
}
var cache: [Int] = [Int](repeating: 0, count: n+1)
cache[1] = 1
cache[2] = 2
for i in 3...n {
cache[i] = cache[i-1] + cache[i-2]
}
return cache[n]
}
}
'PS > LeetCode' 카테고리의 다른 글
Word Break (0) | 2025.02.03 |
---|---|
300. Longest Increasing Subsequence (0) | 2025.02.02 |
Container With Most Water (0) | 2025.01.12 |
3Sum (0) | 2025.01.12 |
Search in Rotated Sorted Array (0) | 2025.01.09 |