forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclimb.py
More file actions
62 lines (46 loc) · 1.54 KB
/
climb.py
File metadata and controls
62 lines (46 loc) · 1.54 KB
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
#### Analysis further required - ****
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
# 4 Line solution from discussions - 100 pass
x = y = 1
for i in range(n):
x, y = y, x+y
return x
"""
# 100 pass
# with reference from solution - recursive solution works best + memoization
# dynamic programming problem as well
# 0 1 2 3 5 8 13
def recurse_stairs(n):
if n == 0:
return 0
if n == 1:
return 1
if n == 2:
return 2
if len(values)>n:
return values[n]
value = recurse_stairs(n-1) + recurse_stairs(n-2)
values.append(value)
return value
# memoize
values = [0,1,2]
return recurse_stairs(n)
"""
"""
# Attempt became more complex hence stopped
# every time you climb only 1 or 2 steps
# Create i for i in range(n) => with only 2s or with only 1s or with both
# All ones scenario - by default include 1 everytime
count = 1
# All 2s scenario
if n%2 == 0:
count += n//2
# Both ones and twos combination
if n%2 == 0:
# 14 => 7 2s => 6,1 - 5,2 - 4,3 - 3,4 - 2,5 - 1,6 => 7*6
"""