Skip to content

Commit 787b7d9

Browse files
committed
Add solution #70
1 parent 027f65d commit 787b7d9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|
5454
66|[Plus One](./0066-plus-one.js)|Easy|
5555
67|[Add Binary](./0067-add-binary.js)|Easy|
56+
70|[Climbing Stairs](./0070-climbing-stairs.js)|Easy|
5657
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
5758
74|[Search a 2D Matrix](./0074-search-a-2d-matrix.js)|Medium|
5859
77|[Combinations](./0077-combinations.js)|Medium|

solutions/0070-climbing-stairs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 70. Climbing Stairs
3+
* https://leetcode.com/problems/climbing-stairs/
4+
* Difficulty: Easy
5+
*
6+
* You are climbing a staircase. It takes n steps to reach the top.
7+
*
8+
* Each time you can either climb 1 or 2 steps. In how many distinct
9+
* ways can you climb to the top?
10+
*/
11+
12+
/**
13+
* @param {number} n
14+
* @return {number}
15+
*/
16+
var climbStairs = function(n) {
17+
const cache = new Map();
18+
const memo = n => {
19+
if (n < 4) return n;
20+
if (!cache.has(n)) {
21+
cache.set(n, memo(n - 2) + memo(n - 1));
22+
}
23+
return cache.get(n);
24+
};
25+
return memo(n);
26+
};

0 commit comments

Comments
 (0)