Skip to content

Commit d7ddf40

Browse files
committed
no message
1 parent 891b50d commit d7ddf40

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

problems/cheapest-flights-within-k-stops.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,36 @@ def findCheapestPrice(self, n, flights, src, dst, K):
6666

6767
return min_price if min_price!=float('inf') else -1
6868

69+
70+
"""
71+
Standard Dijkstra, except this time instead of only explore the ones with least price
72+
We also need to explore the ones with less steps. So add stepFromSrc to check.
73+
74+
Time: O(ELogE), since there will be at most E edges that get pushed into the heap.
75+
Space: O(E)
76+
"""
77+
class Solution(object):
78+
def findCheapestPrice(self, n, flights, src, dst, K):
79+
priceFromSrc = {}
80+
stepFromSrc = {}
81+
h = [(0, 0, src)]
82+
G = collections.defaultdict(list)
83+
84+
#build graph
85+
for s, d, p in flights:
86+
G[s].append((d, p))
87+
88+
#dijkstra
89+
while h:
90+
price, k, node = heapq.heappop(h)
91+
92+
if node==dst: return price
93+
if k>K: continue
94+
95+
for nei, price2 in G[node]:
96+
if nei not in priceFromSrc or price+price2<=priceFromSrc[nei] or k<stepFromSrc[nei]:
97+
priceFromSrc[nei] = price+price2
98+
stepFromSrc[nei] = k
99+
heapq.heappush(h, (price+price2, k+1, nei))
100+
101+
return -1

problems/maximum-average-subtree.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Recursivly get the average and count of each node.
3+
Update the self.ans before returning.
4+
5+
Time: O(N), N is the number of nodes.
6+
Space: O(LogN) for the recursive stack, since the tree hight is LogN if the tree is balanced.
7+
"""
8+
class Solution(object):
9+
def __init__(self):
10+
self.ans = float('-inf')
11+
12+
def maximumAverageSubtree(self, root):
13+
def getAVGAndCount(node):
14+
if not node: return 0, 0
15+
leftAvg, leftCount = getAVGAndCount(node.left)
16+
rightAvg, rightCount = getAVGAndCount(node.right)
17+
18+
count = leftCount+rightCount+1
19+
avg = (leftAvg*leftCount + rightAvg*rightCount + node.val)/float(count)
20+
21+
self.ans = max(self.ans, avg)
22+
return avg, count
23+
24+
getAVGAndCount(root)
25+
return self.ans
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
We know that, after swapping, the length of the continuous 1 will be equal to number of 1 in the data. Let's call it `L`.
3+
So what we need to do is sliding window with length L and see what is the max 1 count within the window.
4+
The window with max 1 count will need to do minimal swap.
5+
"""
6+
class Solution(object):
7+
def minSwaps(self, data):
8+
L = 0 #sliding window length
9+
#initialize L
10+
for num in data:
11+
if num==1: L += 1
12+
13+
14+
count = 0 #count of 1 within the sliding window
15+
maxCount = 0 #max count of 1 within the sliding window
16+
17+
#initialize the count from data[0] to data[L-1]
18+
for i in xrange(L):
19+
if data[i]==1: count += 1
20+
maxCount = count
21+
22+
#slide the window
23+
for i in xrange(1, len(data)):
24+
j = i+L-1 #end index of the sliding window
25+
if j>=len(data): break
26+
if data[j]==1: count += 1
27+
if data[i-1]==1: count -= 1
28+
maxCount = max(maxCount, count)
29+
30+
return L-maxCount

problems/shortest-path-to-get-food.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def getFood(self, grid):
3+
q = collections.deque()
4+
visited = set()
5+
6+
for i in xrange(len(grid)):
7+
for j in xrange(len(grid[0])):
8+
if grid[i][j]=='*':
9+
q.append((i, j, 0))
10+
break
11+
while q:
12+
i, j, steps = q.popleft()
13+
14+
if not 0<=i<len(grid): continue
15+
if not 0<=j<len(grid[0]): continue
16+
if (i, j) in visited: continue
17+
visited.add((i, j))
18+
19+
if grid[i][j]=='#':
20+
return steps
21+
elif grid[i][j]=='X':
22+
continue
23+
elif grid[i][j]=='O' or grid[i][j]=='*':
24+
q.append((i+1, j, steps+1))
25+
q.append((i-1, j, steps+1))
26+
q.append((i, j+1, steps+1))
27+
q.append((i, j-1, steps+1))
28+
29+
return -1

0 commit comments

Comments
 (0)