1
+ /*
2
+ * Problem: 70. Climbing Stairs
3
+ * Acceptance Rate: 49.1%
4
+ * URL: https://leetcode.com/problems/climbing-stairs/
5
+ *
6
+ * Runtime: 76 ms, faster than 79.27% of TypeScript online submissions for Climbing Stairs.
7
+ * Memory Usage: 40.3 MB, less than 49.45% of TypeScript online submissions for Climbing Stairs.
8
+ */
9
+
10
+ export function climbStairsBF ( steps : number ) : number {
11
+ return Solution . countWaysToClimbBF ( steps ) ;
12
+ }
13
+
14
+ export function climbStairsDP ( steps : number ) : number {
15
+ return Solution . countWaysToClimbDP ( steps ) ;
16
+ }
17
+
18
+ export function climbStairsBU ( steps : number ) : number {
19
+ return Solution . countWaysToClimbBU ( steps ) ;
20
+ }
21
+
22
+ class Solution {
23
+ private static numWaysToClimb = 0 ;
24
+ private static waysToDescendFromNthStep = [ 0 , 1 , 2 ] ;
25
+
26
+ static countWaysToClimbDP ( steps : number ) : number {
27
+ return Solution . climbDP ( steps ) ;
28
+ }
29
+
30
+ static countWaysToClimbBU ( steps : number ) : number {
31
+ return Solution . climbBU ( steps ) ;
32
+ }
33
+
34
+ //#region DynamicProgramming
35
+
36
+ private static climbDP ( stairCount : number ) : number {
37
+ if ( Solution . waysToDescendFromNthStep [ stairCount ] !== undefined ) return Solution . waysToDescendFromNthStep [ stairCount ] ; // Check cache
38
+
39
+ // if(stairCount===2) return 2;
40
+ // if(stairCount===1) return 1;
41
+
42
+ let countLeft = Solution . climbDP ( stairCount - 1 ) ;
43
+ let countRight = stairCount - 2 < 1 ? 0 : Solution . climbDP ( stairCount - 2 ) ;
44
+
45
+ let numWaysToDescend = countLeft + countRight ;
46
+ Solution . waysToDescendFromNthStep [ stairCount ] = numWaysToDescend ; //Memoize
47
+
48
+ return numWaysToDescend ;
49
+ }
50
+
51
+ private static climbBU ( stairCount : number ) : number {
52
+
53
+ for ( let i = 3 ; i <= stairCount ; ++ i ) {
54
+ Solution . waysToDescendFromNthStep [ i ] = Solution . waysToDescendFromNthStep [ i - 1 ] + Solution . waysToDescendFromNthStep [ i - 2 ] ;
55
+ }
56
+
57
+ return Solution . waysToDescendFromNthStep [ stairCount ] ;
58
+ }
59
+
60
+ //#endregion
61
+
62
+ //#region BruteForce
63
+
64
+ static countWaysToClimbBF ( steps : number ) : number {
65
+ Solution . climbBF ( steps ) ;
66
+
67
+ let result = Solution . numWaysToClimb ;
68
+
69
+ Solution . numWaysToClimb = 0 ; // Reset state for next call
70
+
71
+ return result ;
72
+ }
73
+
74
+ private static climbBF ( stairCount : number ) {
75
+ if ( stairCount === 0 ) {
76
+ Solution . numWaysToClimb += 1 ;
77
+ } else {
78
+ Solution . climbBF ( stairCount - 1 ) ;
79
+ if ( stairCount - 2 >= 0 ) Solution . climbBF ( stairCount - 2 ) ;
80
+ }
81
+ }
82
+
83
+ //#endregion
84
+ }
85
+
86
+ //---------------------------------------------------------------------
87
+ // ---------- MAIN PROGRAM ----------
88
+ //---------------------------------------------------------------------
89
+ if ( import . meta. main ) {
90
+
91
+ console . log ( "2 Steps:" , climbStairsBF ( 2 ) ) ; // 2
92
+ console . log ( "4 Steps:" , climbStairsBF ( 4 ) ) ; // 5
93
+ console . log ( "8 Steps:" , climbStairsBF ( 8 ) ) ; // 34
94
+
95
+ console . log ( "8 Steps:" , climbStairsDP ( 8 ) ) ; // Dynamic programming approach
96
+
97
+ console . log ( "8 Steps:" , climbStairsBU ( 8 ) ) ; // Bottom up approach
98
+
99
+ // RUN: deno run Easy/ClimbingStairs.ts
100
+ }
101
+
102
+ // --------------------------- Terminal Output: ---------------------------
103
+ // 2 Steps: 2
104
+ // 4 Steps: 5
105
+ // 8 Steps: 34
106
+ // 8 Steps: 34
107
+ // 8 Steps: 34
0 commit comments