-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimb_stairs.py
53 lines (45 loc) · 1.17 KB
/
climb_stairs.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 21:50:48 2019
@author: Himanshu
"""
from collections import Counter
words = "if there was there was but if there was not there was not".split()
print(words)
counts = Counter(words)
print(counts.most_common(3))
for i in counts:
print(counts[i])
#print(counts)
# List comprehension
#test = sum([i * i for i in range(1, 10000001)])
#print(test)
# Generator
test = sum((i * i for i in range(1, 100001)))
#print(test)
class Solution(object):
"""
Explanation:
This problem is the Fibonacci sequence in disguise
You have a recurrence relationship that is
S_i = S_(i-1) + S_(i-2)
and you have to solve it without blowing the stack,
i.e. save the solution to previous sub problems
O(n) Time
O(n) Space
"""
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
steps = [0 for _ in range(n + 1)]
# breakpoint()
for num in (0,1,2):
steps[num] = num
for num in range(3, n+1):
steps[num] = steps[num-1] + steps[num-2]
return steps[n]
r = Solution()
res = r.climbStairs(5)
#print (res)