From b7006c8a78d249763881d1b2fa9d577dcf6cb7e1 Mon Sep 17 00:00:00 2001 From: Hongseoup Yun Date: Fri, 21 Nov 2025 11:08:50 -0500 Subject: [PATCH 1/2] feat: add climbing stairs solution with detailed comments on approach and complexity --- climbing-stairs/hongseoupyun.py | 38 +++++++++++++++++++++++++++++++++ climbing-stairs/hongseoupyun.ts | 36 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 climbing-stairs/hongseoupyun.py create mode 100644 climbing-stairs/hongseoupyun.ts diff --git a/climbing-stairs/hongseoupyun.py b/climbing-stairs/hongseoupyun.py new file mode 100644 index 0000000000..f5ab358ce0 --- /dev/null +++ b/climbing-stairs/hongseoupyun.py @@ -0,0 +1,38 @@ + +# 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): + current = prev2 + prev1 + prev1 = prev2 + prev2 = current + + return prev2 + + + + + + + + + + \ No newline at end of file diff --git a/climbing-stairs/hongseoupyun.ts b/climbing-stairs/hongseoupyun.ts new file mode 100644 index 0000000000..582acd8374 --- /dev/null +++ b/climbing-stairs/hongseoupyun.ts @@ -0,0 +1,36 @@ +// # 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 +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 +}; + + + + + From 0e1375d312ef35a1b7c65d8dfc062a7d4f4845d5 Mon Sep 17 00:00:00 2001 From: Hongseoup Yun Date: Fri, 21 Nov 2025 11:14:45 -0500 Subject: [PATCH 2/2] refactor: remove unnecessary blank lines in climbing stairs implementations --- climbing-stairs/hongseoupyun.py | 10 ---------- climbing-stairs/hongseoupyun.ts | 5 ----- 2 files changed, 15 deletions(-) diff --git a/climbing-stairs/hongseoupyun.py b/climbing-stairs/hongseoupyun.py index f5ab358ce0..15641fc9d9 100644 --- a/climbing-stairs/hongseoupyun.py +++ b/climbing-stairs/hongseoupyun.py @@ -26,13 +26,3 @@ def climbStairs(self, n: int) -> int: prev2 = current return prev2 - - - - - - - - - - \ No newline at end of file diff --git a/climbing-stairs/hongseoupyun.ts b/climbing-stairs/hongseoupyun.ts index 582acd8374..7cebb26f92 100644 --- a/climbing-stairs/hongseoupyun.ts +++ b/climbing-stairs/hongseoupyun.ts @@ -29,8 +29,3 @@ function climbStairs(n: number): number { return prev2 }; - - - - -