Skip to content

Commit f96764a

Browse files
author
Chris Wu
committed
no message
1 parent 3920ee2 commit f96764a

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

problems/climbing-stairs.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,24 @@ def factorial(int_num):
4343
int_num-=1
4444
return counter
4545

46-
return factorial(m)/(factorial(n)*factorial(m-n))
46+
return factorial(m)/(factorial(n)*factorial(m-n))
47+
48+
#2020/11/9
49+
class Solution(object):
50+
def climbStairs(self, n):
51+
def helper(n):
52+
if n in history: return history[n]
53+
54+
if n==0 or n==1:
55+
history[n] = 1
56+
elif n==2:
57+
return 2
58+
elif n>2:
59+
#combination count of n stairs equals to
60+
#(the combination after you make 1 step as first move) + (the combination after you make 2 steps as first move)
61+
history[n] = helper(n-1) + helper(n-2)
62+
63+
return history[n]
64+
65+
history = {}
66+
return helper(n)

problems/min-cost-climbing-stairs.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,23 @@ def minCostClimbingStairs(self, cost):
2020
else:
2121
memo[i] = min(memo[i-1]+cost[i-1], memo[i-2]+cost[i-2])
2222

23-
return memo[-1]
23+
return memo[-1]
24+
25+
#2020/11/9
26+
class Solution(object):
27+
def minCostClimbingStairs(self, cost):
28+
def helper(i):
29+
if i in history: return history[i]
30+
31+
if i>len(cost)-1:
32+
history[i] = 0
33+
elif i==len(cost)-1 or i==len(cost)-2:
34+
history[i] = cost[i]
35+
elif i<len(cost)-2:
36+
history[i] = cost[i] + min(helper(i+1), helper(i+2))
37+
38+
return history[i]
39+
40+
history = {}
41+
return min(helper(0), helper(1))
42+

problems/reconstruct-itinerary.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#build graph with departure city as key and (destination, ticket_id) as value
2+
#G = build_graph()
3+
#itinerary = ['JFK']
4+
#ticket_used = set()
5+
#dfs()
6+
#if len(itinerary)==len(tickets)+1: return True
7+
#here = itinerary[-1]
8+
#if here not in G: return False
9+
#candidates = nei, ticket_id for nei, ticket_id in G[here] if ticket_id not in ticket_used
10+
#for nei, ticket_id in candidates:
11+
#ticket_used.add(ticket_id)
12+
#itinerary.append(nei)
13+
#if dfs(): return True
14+
#ticket_used.remove(ticket_id)
15+
#itinerary.pop()
16+
#return False
17+
18+
import collections
19+
20+
class Solution(object):
21+
def findItinerary(self, tickets):
22+
def dfs():
23+
if len(itinerary)==len(tickets)+1: return True
24+
here = itinerary[-1]
25+
if here not in G: return False
26+
27+
candidates = [(nei, ticket_id) for nei, ticket_id in G[here] if ticket_id not in ticket_used]
28+
29+
for nei, ticket_id in candidates:
30+
ticket_used.add(ticket_id)
31+
itinerary.append(nei)
32+
if dfs(): return True
33+
ticket_used.remove(ticket_id)
34+
itinerary.pop()
35+
return False
36+
37+
G = collections.defaultdict(list)
38+
itinerary = ['JFK']
39+
ticket_used = set()
40+
41+
#build graph
42+
ticket_id_counter = 0
43+
for n1, n2 in tickets:
44+
G[n1].append((n2, ticket_id_counter))
45+
ticket_id_counter += 1
46+
for k in G: G[k].sort(key=lambda x:x[0])
47+
48+
return dfs()

0 commit comments

Comments
 (0)