Skip to content

Commit 6ef3bee

Browse files
committed
Merge remote-tracking branch 'upstream/main'
merge to latest head
2 parents fe64669 + 08a3a2b commit 6ef3bee

File tree

117 files changed

+3932
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+3932
-36
lines changed

climbing-stairs/HC-kang.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// T.C. O(n)
2+
// S.C. O(1)
3+
function climbStairs(n: number): number {
4+
let p = 0;
5+
let q = 1;
6+
for (let i = 0; i < n; i++) {
7+
q = q + p;
8+
p = q - p;
9+
}
10+
return q;
11+
}

climbing-stairs/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
// Time complexity: O(n);
4+
// Space complexity: O(n);
5+
int[] dp = new int[n+1];
6+
7+
if (n >= 1) dp[1] = 1;
8+
if (n >= 2) dp[2] = 2;
9+
10+
for (int i = 3; i <= n; i++) {
11+
dp[i] = dp[i-1] + dp[i-2];
12+
}
13+
return dp[n];
14+
}
15+
}

climbing-stairs/bemelon.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# Space complexity: O(1)
3+
# Tiem complexity: O(n)
4+
def climbStairs(self, n: int) -> int:
5+
# dp[0] is n - 2
6+
# dp[1] is n - 1
7+
dp = [1, 2]
8+
9+
if n <= 2:
10+
return dp[n - 1]
11+
12+
for i in range(3, n + 1):
13+
# dp[n] = dp[n - 1] + dp[n - 2]
14+
# = dp[1] + dp[0]
15+
dp[(i - 1) % 2] = sum(dp)
16+
17+
return dp[(n - 1) % 2]

climbing-stairs/flynn.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int climbStairs(int n) {
4+
vector<int> memo(2, 1);
5+
6+
for (int i = 2; i <= n; i++) {
7+
memo.push_back(memo[i - 1] + memo[i - 2]);
8+
}
9+
10+
return memo[n];
11+
}
12+
13+
};

climbing-stairs/gitsunmin.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/climbing-stairs
3+
* time complexity : O(n)
4+
* space complexity : O(1)
5+
*/
6+
7+
export const upStairs = (n: number): number => {
8+
let [l, r] = [1, 2];
9+
for (let i = 3; i <= n; i++) [l, r] = [r, l + r];
10+
11+
return r;
12+
};
13+
14+
export function climbStairs(n: number): number {
15+
if (n <= 2) return n;
16+
return upStairs(n);
17+
};

climbing-stairs/haklee.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""TC: O(n), SC: O(1)
2+
3+
아이디어:
4+
계단의 k번째 칸까지 도달하는 방법의 수를 f(k)라고 하자.
5+
f(k)는 다음의 두 경우의 수를 더한 값이다.
6+
- k-2번째 칸까지 간 다음 두 칸 뜀. 즉, f(k-2)
7+
- k-1번째 칸까지 간 다음 두 칸 뜀. 즉, f(k-1)
8+
즉, f(k) = f(k-2) + f(k-1)
9+
10+
11+
SC:
12+
- tabulation 과정에서 값 2개만 계속 유지한다.
13+
- 즉, O(1).
14+
15+
TC:
16+
- 단순 덧셈 계산(O(1))을 O(n)번 반복한다.
17+
- 즉, O(n).
18+
"""
19+
20+
21+
class Solution:
22+
def climbStairs(self, n: int) -> int:
23+
a, b = 1, 1
24+
for _ in range(n - 1):
25+
a, b = b, a + b
26+
return b

climbing-stairs/heozeop.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time Complexity: O(n)
2+
// Spatial Complexity: O(n)
3+
4+
class Solution {
5+
public:
6+
int climbStairs(int n) {
7+
vector<int> dp(n + 1, 0);
8+
9+
if (n == 1) {
10+
return 1;
11+
}
12+
13+
dp[0] = dp[1] = 1;
14+
for(int i = 2; i <= n; ++i) {
15+
dp[i] = dp[i - 1] + dp[i - 2];
16+
}
17+
18+
return dp[n];
19+
}
20+
};

climbing-stairs/highball.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const climbStairs = function (n) {
2+
const dp = [1, 1];
3+
4+
for (let i = 0; i < n - 1; i++) {
5+
const temp = dp[1];
6+
dp[1] = temp + dp[0];
7+
dp[0] = temp;
8+
}
9+
10+
return dp[1];
11+
};

climbing-stairs/hwanmini.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 시간복잡도 O(n)
2+
// 공간복잡도 O(n)
3+
4+
/**
5+
* @param {number} n
6+
* @return {number}
7+
*/
8+
var climbStairs = function(n) {
9+
const stairs = [1, 2]
10+
11+
for (let i = 2; i < n; i++) {
12+
stairs[i] = stairs[i-1] + stairs[i-2]
13+
}
14+
15+
return stairs[n-1]
16+
};
17+
18+
console.log(climbStairs(5))

climbing-stairs/hyejjun.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var climbStairs = function (n) {
6+
if (n <= 2) return n;
7+
8+
let first = 1;
9+
let second = 2;
10+
11+
for (let i = 3; i <= n; i++) {
12+
let third = first + second;
13+
first = second;
14+
second = third;
15+
}
16+
17+
return second;
18+
};
19+
20+
console.log(climbStairs(2));
21+
console.log(climbStairs(3));
22+
console.log(climbStairs(4));
23+
console.log(climbStairs(5));
24+
25+
/*
26+
시간 복잡도: O(n)
27+
공간 복잡도: O(1)
28+
*/

0 commit comments

Comments
 (0)