Skip to content

Commit fd0d169

Browse files
committed
Completed '70. Climbing Stairs' (Dynamic Programming)
1 parent 9ec2f4f commit fd0d169

5 files changed

+175
-3
lines changed

Easy/ClimbingStairs.ts

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Easy/test/ClimbingStairs.test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { assertEquals } from '../../test_deps.ts';
2+
3+
import { climbStairsBF, climbStairsDP, climbStairsBU } from '../ClimbingStairs.ts';
4+
5+
6+
// RUN: deno test Easy/test/ClimbingStairs.test.ts
7+
8+
//---------------------------------------------------------------------
9+
// ---------- UNIT TESTS ----------
10+
//---------------------------------------------------------------------
11+
12+
Deno.test({
13+
name: "BF: 3 steps -> 3 ways",
14+
fn() {
15+
assertEquals(climbStairsBF(3), 3);
16+
}
17+
});
18+
19+
Deno.test({
20+
name: "BF: 4 steps -> 5 ways",
21+
fn() {
22+
assertEquals(climbStairsBF(4), 5);
23+
}
24+
});
25+
26+
Deno.test({
27+
name: "BF: 8 steps -> 34 ways",
28+
fn() {
29+
assertEquals(climbStairsBF(8), 34);
30+
}
31+
});
32+
33+
Deno.test({
34+
name: "DP: 8 steps -> 34 ways",
35+
fn() {
36+
assertEquals(climbStairsDP(8), 34);
37+
}
38+
});
39+
40+
Deno.test({
41+
name: "BU: 8 steps -> 34 ways",
42+
fn() {
43+
assertEquals(climbStairsBU(8), 34);
44+
}
45+
});
46+
47+
Deno.test({
48+
name: "DP: 44 steps -> 1,113,903,170 ways",
49+
fn() {
50+
assertEquals(climbStairsDP(44), 1_134_903_170);
51+
}
52+
});
53+
54+
Deno.test({
55+
name: "BU: 44 steps -> 1,113,903,170 ways",
56+
fn() {
57+
assertEquals(climbStairsBU(44), 1_134_903_170);
58+
}
59+
});

Medium/LongestPalindromicSubstring.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* URL: https://leetcode.com/problems/longest-palindromic-substring/
55
*
66
* Runtime: 96 ms, faster than 96.82% of TypeScript online submissions for Longest Palindromic Substring.
7-
* Memory Usage: 41.9 MB, less than 65.68% of TypeScript online submissions for Longest Palindromic Substring.
7+
* Memory Usage: 41.9 MB, less than 65.68% of TypeScript online submissions for Longest Palindromic Substring.
88
*/
99

1010
export default function longestPalindromeIn(s: string): string {

Medium/test/LongestPalindromicSubstring.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assertEquals, assertNotEquals } from '../../test_deps.ts';
33
import longestPalindromeIn from '../LongestPalindromicSubstring.ts';
44

55

6-
// RUN: deno test medium/test/LongestPalindromicSubstring.test.ts
6+
// RUN: deno test Medium/test/LongestPalindromicSubstring.test.ts
77

88
//---------------------------------------------------------------------
99
// ---------- UNIT TESTS ----------

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# LeetCode Solutions in TypeScript
22

3+
LeetCode Profile – [Ryo112358](https://leetcode.com/Ryo112358/)
4+
35
## Environment
46
- <span title="July 2021">Deno 1.12.0</span>
57
- V8 9.2.230.14
@@ -9,5 +11,9 @@
911

1012
Java Solutions – [CoffeelessProgrammer/leetcode-java](https://github.com/CoffeelessProgrammer/leetcode-java)
1113

14+
### Easy
15+
16+
- <span title="Acceptance Rate: 49.1%">70. Climbing Stairs</span>
17+
1218
### Medium
13-
- <span title="Acceptance Rate: 31.0%">5. Longest Palindromic Substring</span>
19+
- <span title="Acceptance Rate: 31.0%">5. Longest Palindromic Substring</span>

0 commit comments

Comments
 (0)