-
Notifications
You must be signed in to change notification settings - Fork 376
/
climbing_stairs_test.go
70 lines (58 loc) · 1.15 KB
/
climbing_stairs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Problem:
- You are climbing a staircase. It takes n steps to reach to the top.
- Each time you can either climb 1 or 2 steps. In how many distinct ways
can you climb to the top.
Example:
- Input: 2
Output: 2
Explanation: Two ways to climb the top are 1+1 and 2.
- Input: 3
Output: 3
Explanation: Two ways to climb the top are 1+1+1, 1+2, and 2+1.
Approach:
- To reach n step, one either advance one step from the n-1 step or
2 steps from the n-2 step.
- This is basically the same recurrence formula defined by the Fibonacci
sequence.
Solution:
- Initialize two variables to store 2 previous value.
- Iterate from 2 to n and update these two values at each step.
Cost:
- O(n) time, O(1) space.
*/
package leetcode
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestClimbStairs(t *testing.T) {
tests := []struct {
in int
expected int
}{
{0, 1},
{1, 1},
{2, 2},
{3, 3},
{4, 5},
{5, 8},
{6, 13},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
climbStairs(tt.in),
)
}
}
func climbStairs(n int) int {
p, q := 1, 1
for i := 2; i <= n; i++ {
tmp := q
q += p
p = tmp
}
return q
}