Skip to content

Commit acd198a

Browse files
committed
70
1 parent 43141e9 commit acd198a

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
101101
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/#/description)| [Python [Yu]](./backtrack/Yu/39.py) | _O(K * (2^N)_| _O(N)_ | Medium | |[公瑾讲解](https://youtu.be/HdS5dOaz-mk)|
102102
|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/#/solutions)| [Python [Yu]](./backtrack/Yu/39.py) | _O(K * (2^N)_| _O(N)_ | Medium | |[公瑾讲解](https://youtu.be/HdS5dOaz-mk)|
103103
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/#/description)| [Python [Yu]](./backtrack/Yu/216.py) | _O(K * (2^N)_| _O(N)_ | Medium | ||
104+
105+
106+
## Dynamic Programming Easy
107+
| # | Title | Solution | Time | Space | Difficulty | Note|
108+
|-----|-------| -------- | ---- | ------|------------|-----|
109+
|70|[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/#/solutions)| [Python [Yu]](./backtrack/Yu/216.py) | _O(N)_| _O(1)_ | Easy| |

dp/70.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 70. Climbing Stairs
2+
# Author: Yu Zhou
3+
4+
# Each time you can either climb 1 or 2 steps.
5+
# 各种方法
6+
# Bottom Up就是从最基本的base case然后慢慢的解决所有的subproblem,指导累计至N
7+
# 这道题因为只Care N-1 和 N-2 两个值
8+
# 可以直接用O(1)的Space,具体操作就是对两个函数进行更改,而不是存Hash或者Array
9+
10+
11+
# Top Down Recursion (超时)
12+
class Solution(object):
13+
def climbStairs(self, n):
14+
# Base case:
15+
if n == 1:
16+
return 1
17+
if n == 2:
18+
return 2
19+
return self.climbStairs(n-1) + self.climbStairs(n-2)
20+
21+
# Top Down Memorization DP (Hash表)
22+
class Solution(object):
23+
def __init__(self):
24+
self.hash = {1:1, 2:2}
25+
26+
def climbStairs(self, n):
27+
"""
28+
:type n: int
29+
:rtype: int
30+
"""
31+
if n not in self.hash:
32+
self.hash[n] = self.climbStairs(n-1) + self.climbStairs(n-2)
33+
return self.hash[n]
34+
35+
# Bottom-up DP (ArrayList)
36+
class Solution(object):
37+
def climbStairs(self, n):
38+
"""
39+
:type n: int
40+
:rtype: int
41+
"""
42+
if n == 1:
43+
return 1
44+
45+
res = [1,2]
46+
for i in xrange(2, n):
47+
res.append(res[i-1] + res[i-2])
48+
return res[-1]
49+
50+
# Bottom-up DP (Constant)
51+
class Solution(object):
52+
def climbStairs(self, n):
53+
"""
54+
:type n: int
55+
:rtype: int
56+
"""
57+
if n == 1:
58+
return 1
59+
60+
a, b = 1, 2
61+
for i in xrange(2, n):
62+
temp = b
63+
b = b + a
64+
a = temp
65+
66+
return b

try.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
# Creates a list containing 5 lists, each of 8 items, all set to 0
2-
n = 6;
3-
Matrix = [["." for x in range(n)] for y in range(n)]
4-
string = ','.join(Matrix)
5-
6-
#print Matrix
7-
print string
1+
lst = [[0] * 3 for _ in xrange(2)]
2+
ret = ', '.join(str(x) for x in lst)
3+
print (str(lst))
4+
print ret

0 commit comments

Comments
 (0)