Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions climbing-stairs/hongseoupyun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

# This problem is a getting the number of cases to climb stairs just taking 1 or 2 steps at a time.
# The pattern is similar to Fibonacci sequence.
# As n increases by 1, the number of ways to climb will be 1, 2, 3, 5, 8, 13, 21
# The Formula is to get number of ways to climb to the nth step is:
# ways(n) = ways(n-1) + ways(n-2) with base of ways(1) = 1 and ways(2) = 2
# which means the number of ways to climb is the sum of last two previous ways



# Time complexity is O(n) - loops n times
# Space complexity is O(1) — memorizes only prev1 & prev2
class Solution:
def climbStairs(self, n: int) -> int:
# Handling base case
if n <= 1:
return 1
if n == 2:
return 2

prev1 = 1
prev2 = 2
for i in range(n,n+1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range(n, n+1)이라면 루프가 딱 한 번만 실행될 것 같아요! range(3, n+1)을 쓰시려다 오타가 생긴 것 같습니다 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 제가 미쳐보지못한 실수네요.. 감사합니다!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current = prev2 + prev1
prev1 = prev2
prev2 = current

return prev2
31 changes: 31 additions & 0 deletions climbing-stairs/hongseoupyun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// # This problem is a getting the number of cases to climb stairs just taking 1 or 2 steps at a time.
// # The pattern is similar to Fibonacci sequence.
// # As n increases by 1, the number of ways to climb will be 1, 2, 3, 5, 8, 13, 21
// # The Formula is to get number of ways to climb to the nth step is:
// # ways(n) = ways(n-1) + ways(n-2) with base of ways(1) = 1 and ways(2) = 2
// # which means the number of ways to climb is the sum of last two previous ways


//Time complexity is O(n) - loops n times
//Space complexity is O(1) — memorizes only prev1 & prev2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP 테이블 없이 변수 2개만 유지함으로써 공간 복잡도를 최적화하셨네요! 👍🏻

function climbStairs(n: number): number {


if (n <=1 ) return 1;
if (n === 2) return 2;

// initial settiongs
let prev1 = 1
let prev2 = 2

//Loop from when the number of stairs is 3
for (let i = 3; i<=n; i++){
const current = prev2 + prev1
//eg) when n = 3, prev1 = 1, prev2 = 2, current = 3 => prev1 = 2, prev2 = 3, return prev2
//eg) when n = 4, prev1 = 2, prev2 = 3, current = 5 => prev1 = 3, preev2 = 5, return prev2
prev1 = prev2
prev2 = current
}

return prev2
};