Skip to content

Commit 66a816e

Browse files
authored
Merge pull request #5 from GolubevDS/climbing-stairs
Climbing Stairs
2 parents b7a75ea + 4be5a00 commit 66a816e

File tree

8 files changed

+54
-15
lines changed

8 files changed

+54
-15
lines changed

README.MD

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# LEETCODE PATTERNS
22

3-
---
4-
53
Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetcode-patterns/) website
64

7-
---
85
## Problems
96

107
| Title | Solution | Difficulty |
@@ -13,3 +10,4 @@ Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetc
1310
| [268. Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
1411
| [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
1512
| [136. Single Number](https://leetcode.com/problems/single-number/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13+
| [70. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |

solutions/climbStairs/README.MD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## [70. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)
2+
3+
You are climbing a staircase. It takes `n` steps to reach the top.
4+
5+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
6+
7+
#### Constraints:
8+
9+
`1 <= n <= 45`

solutions/climbStairs/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const cache = [];
2+
3+
/**
4+
* @param {number} n
5+
* @return {number}
6+
*/
7+
function climbStairs(n) {
8+
if (n <= 3) {
9+
return n;
10+
}
11+
12+
if (!cache[n]) {
13+
cache[n] = climbStairs(n - 2) + climbStairs(n - 1);
14+
}
15+
16+
return cache[n];
17+
}
18+
19+
module.exports = climbStairs;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const climbStairs = require('./index');
2+
3+
describe('Climbing Stairs', () => {
4+
it('1. Input: n = 1', () => {
5+
const result = climbStairs(1);
6+
const expected = 1;
7+
expect(result).toStrictEqual(expected);
8+
});
9+
10+
it('2. Input: n = 4', () => {
11+
const result = climbStairs(4);
12+
const expected = 5;
13+
expect(result).toStrictEqual(expected);
14+
});
15+
16+
it('3. Input: n = 45', () => {
17+
const result = climbStairs(45);
18+
const expected = 1836311903;
19+
expect(result).toStrictEqual(expected);
20+
});
21+
});
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
### [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
2-
3-
---
1+
## [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
42

53
Given an integer array `nums`, return `true` if any value appears at least twice in the array, and return `false` if every element is distinct.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
### [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)
2-
3-
---
1+
## [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)
42

53
Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return an array of all the integers in the range `[1, n]` that do not appear in `nums`.

solutions/missingNumber/README.MD

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
### [268. Missing Number](https://leetcode.com/problems/missing-number/)
2-
3-
---
1+
## [268. Missing Number](https://leetcode.com/problems/missing-number/)
42

53
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return the only number in the range that is missing from the array.

solutions/singleNumber/README.MD

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
### [136. Single Number](https://leetcode.com/problems/single-number/)
2-
3-
---
1+
## [136. Single Number](https://leetcode.com/problems/single-number/)
42

53
Given a **non-empty** array of integers `nums`, every element appears twice except for one. Find that single one.
64

0 commit comments

Comments
 (0)