diff --git a/climbing-stairs/hongseoupyun.py b/climbing-stairs/hongseoupyun.py new file mode 100644 index 0000000000..15641fc9d9 --- /dev/null +++ b/climbing-stairs/hongseoupyun.py @@ -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): + current = prev2 + prev1 + prev1 = prev2 + prev2 = current + + return prev2 diff --git a/climbing-stairs/hongseoupyun.ts b/climbing-stairs/hongseoupyun.ts new file mode 100644 index 0000000000..7cebb26f92 --- /dev/null +++ b/climbing-stairs/hongseoupyun.ts @@ -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 +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 +};