๐ป Coding Problems Solving/Dynamic Programming
[LeetCode] Climbing Stairs
Kim_dev
2022. 7. 9. 19:56
[LeetCode] Climbing Stairs
1. ๋ฌธ์ : Link
2. ํ์ด
์ด์ ๊ณผ ๊ทธ ์ด์ ๊ฐ์ ํฉ๊ณผ ๊ฐ์์ dp๋ก ํ ์ ์์๋ค.
3. ์ฝ๋
class Solution:
def climbStairs(self, n: int) -> int:
dp = [i for i in range(n+1)]
for i in range(3, n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n];
dp[0] = 1;
if(n==1) return dp[n-1];
dp[1] = 2;
if(n==2) return dp[n-1];
for(int i=2; i<n; i++){
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n-1];
}
}