-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path70_ClimbingStairs.py
119 lines (97 loc) · 3.16 KB
/
70_ClimbingStairs.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# By Will Shin
#
#-------------------------------------------------------------------------------
# LeetCode prompt
#-------------------------------------------------------------------------------
"""
You are climbing a stair case. 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 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
"""
#-------------------------------------------------------------------------------
# Approach
#-------------------------------------------------------------------------------
"""
* There are a few ways to do this, but I want to do the Dynamic Programming approach
* If you start off from 0 then you have 0 ways
* Then you have 1 way for 1
* And 2 ways for 2.
* the remaining are the sum of index n-1 and n-2
[ 0 1 2 3 4 5 6 ] = these are the indices
[ 0 1 2 3 5 8 13 ] = these are the answers
"""
#-------------------------------------------------------------------------------
# Solution
#-------------------------------------------------------------------------------
def my_climbStairs(n):
if n == 0:
return 0
if n == 1:
return 1
memo = [None] * (n+1)
memo[0] = 0
memo[1] = 1
memo[2] = 2
if n > 2:
for i in range(3, n+1):
memo[i] = memo[i-1] + memo[i-2]
return(memo[n])
#-------------------------------------------------------------------------------
# Main Leetcode Input Driver
#-------------------------------------------------------------------------------
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
return my_climbStairs(n)
#-------------------------------------------------------------------------------
# Unit Test
#-------------------------------------------------------------------------------
import unittest
class TestSolution(unittest.TestCase):
def test_0(self):
input = 0
ans = 0
self.assertEqual(Solution().climbStairs(input), ans)
def test_1(self):
input = 1
ans = 1
self.assertEqual(Solution().climbStairs(input), ans)
def test_2(self):
input = 2
ans = 2
self.assertEqual(Solution().climbStairs(input), ans)
def test_3(self):
input = 3
ans = 3
self.assertEqual(Solution().climbStairs(input), ans)
def test_4(self):
input = 4
ans = 5
self.assertEqual(Solution().climbStairs(input), ans)
def test_5(self):
input = 5
ans = 8
self.assertEqual(Solution().climbStairs(input), ans)
def test_6(self):
input = 6
ans = 13
self.assertEqual(Solution().climbStairs(input), ans)
unittest.main()