Skip to content

Commit e487155

Browse files
committed
feat(solutions): add climbing_stairs
1 parent 460ae1c commit e487155

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2727
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
28+
|70|[Climbing Stairs][Solutions-70]|[WindomZ][Solutions-70-go]|Easy|[7.59 ns/op/9 test cases][Solutions-70-Test]|0 ms|
2829
|69|[Sqrt(x)][Solutions-69]|[WindomZ][Solutions-69-go]|Easy|[27.6 ns/op/8 test cases][Solutions-69-Test]|6 ms|
2930
|67|[Add Binary][Solutions-67]|[WindomZ][Solutions-67-Go]|Easy|[84.0 ns/op/5 test cases][Solutions-67-Test]|3 ms|
3031
|66|[Plus One][Solutions-66]|[WindomZ][Solutions-66-Go]|Easy|[29.8 ns/op/6 test cases][Solutions-66-Test]|3 ms|
@@ -123,6 +124,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
123124
### Support
124125
If you like it then you can put a :star:Star on it.
125126

127+
[Solutions-70]:https://leetcode.com/problems/climbing-stairs/
128+
[Solutions-70-go]:solutions/climbing_stairs/climbingstairs.go
129+
[Solutions-70-Test]:solutions/climbing_stairs/climbingstairs_test.go#L17
126130
[Solutions-69]:https://leetcode.com/problems/sqrtx/
127131
[Solutions-69-go]:solutions/sqrtx/sqrtx.go
128132
[Solutions-69-Test]:solutions/sqrtx/sqrtx_test.go#L23

solutions/climbing_stairs/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 70. Climbing Stairs
2+
3+
[Description](https://leetcode.com/problems/climbing-stairs/description/) |
4+
[Discuss](https://leetcode.com/problems/climbing-stairs/discuss/) |
5+
[Solution](https://leetcode.com/problems/climbing-stairs/solution/)
6+
7+
## Description
8+
9+
You are climbing a stair case. It takes _n_ steps to reach to the top.
10+
11+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
12+
13+
**Note:** Given _n_ will be a positive integer.
14+
15+
**Example 1:**
16+
```
17+
Input: 2
18+
Output: 2
19+
Explanation: There are two ways to climb to the top.
20+
21+
1. 1 step + 1 step
22+
2. 2 steps
23+
```
24+
25+
**Example 2:**
26+
```
27+
Input: 3
28+
Output: 3
29+
Explanation: There are three ways to climb to the top.
30+
31+
1. 1 step + 1 step + 1 step
32+
2. 1 step + 2 steps
33+
3. 2 steps + 1 step
34+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package climbingstairs
2+
3+
func climbStairs(n int) int {
4+
// can reach (i) step in one of the two ways:
5+
// 1. taking a single step from (i-1) step.
6+
// 2. taking a step of 2 from (i-2) step.
7+
// so
8+
// 'current' is (i-1) -> (i) step
9+
// 'next' is (i) -> (i+1) step
10+
// reach (i+1) step: (i) step + (i-1) step
11+
current, next := 1, 1
12+
for ; n > 0; n-- {
13+
current, next = next, current+next
14+
}
15+
return current
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package climbingstairs
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_climbStairs(t *testing.T) {
10+
assert.Equal(t, 1, climbStairs(1))
11+
assert.Equal(t, 2, climbStairs(2))
12+
assert.Equal(t, 3, climbStairs(3))
13+
assert.Equal(t, 5, climbStairs(4))
14+
assert.Equal(t, 8, climbStairs(5))
15+
}
16+
17+
func Benchmark_climbStairs(b *testing.B) {
18+
b.StopTimer()
19+
b.ReportAllocs()
20+
b.StartTimer()
21+
b.RunParallel(func(pb *testing.PB) {
22+
for pb.Next() {
23+
climbStairs(1)
24+
climbStairs(2)
25+
climbStairs(3)
26+
climbStairs(4)
27+
climbStairs(5)
28+
climbStairs(6)
29+
climbStairs(7)
30+
climbStairs(8)
31+
climbStairs(9)
32+
}
33+
})
34+
}

0 commit comments

Comments
 (0)