Skip to content

Commit f6fec0e

Browse files
author
weng
committed
minor updates
1 parent 61a2cde commit f6fec0e

12 files changed

+194
-21
lines changed

BST2doubly_llist.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
'''
3+
Leetcode: Convert Binary Search Tree (BST) to Sorted Doubly-Linked List
4+
'''
5+
from __future__ import division
6+
import random
7+
from BinaryTree import *
8+
9+
10+
def get_tail(node):
11+
while node.right:
12+
node = node.right
13+
return node
14+
15+
16+
def convert(root):
17+
if not root: return None
18+
left_list = convert(root.left)
19+
right_list = convert(root.right)
20+
21+
if left_list:
22+
tail = get_tail(left_list)
23+
tail.right = root
24+
root.left = tail
25+
26+
if right_list:
27+
right_list.left = root
28+
root.right = right_list
29+
30+
if left_list: return left_list
31+
else: return root
32+
33+
34+
if __name__ == '__main__':
35+
print BST
36+
head = convert(BST)
37+
38+
# to right
39+
node = head
40+
while node.right:
41+
print node.value, '->',
42+
node = node.right
43+
print node.value
44+
45+
# to right
46+
while node.left:
47+
print node.value, '<-',
48+
node = node.left
49+
print node.value
50+
51+
52+

BinaryTree.pyc

3.13 KB
Binary file not shown.

gas_station.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,39 @@
1111
from __future__ import division
1212
import random
1313

14+
15+
# We can use a Queue to store the current tour. We first enqueue first petrol pump to the queue, we keep enqueueing petrol pumps till we either complete the tour, or current amount of petrol becomes negative. If the amount becomes negative, then we keep dequeueing petrol pumps till the current amount becomes positive or queue becomes empty.
16+
def gas_station(gas, cost):
17+
n = len(gas)
18+
start = 0; next = 1
19+
Q = []
20+
cur_gas = gas[start] - cost[start]
21+
while start != next or cur_gas < 0:
22+
while start != next and cur_gas < 0:
23+
# pop, changing starting point
24+
cur_gas -= gas[start] - cost[start]
25+
start = (start+1)%n
26+
if start == 0: # go back to the first trial
27+
return -1
28+
cur_gas += gas[next] - cost[next]
29+
next = (next+1)%n
30+
return start
31+
32+
33+
'''
34+
What if we want to keep the leftover gas as much as possible?
35+
'''
1436
# G(i,j) = the maximum leftover gas a car goes from i-th station to j-th.
1537
# the car can use gas at i-th but not at j-th
1638
# only valid when G(i,j) >= 0
1739
# G(i,j) = max{G(i,mid)+G(k,mid) for i < mid < j,
1840
# gas[i] - (cost[i]+...+cost[j])}
1941
# We use G(i,i) for a circular route.
20-
# ~ O(n^2)
21-
def gas_station(gas, cost):
42+
# ~ O(n^3)
43+
def gas_station2(gas, cost):
2244
n = len(gas)
2345
G = {}
24-
for i in range(n):
25-
G[i,(i+1)%n] = gas[i] - cost[i]
46+
for i in range(n): G[i,(i+1)%n] = gas[i] - cost[i]
2647
print G
2748

2849
for k in range(2,n+1):
@@ -45,9 +66,60 @@ def gas_station(gas, cost):
4566
return max(circles.values()) >= 0
4667

4768

69+
'''
70+
What if we want to get the trip with the minimum times of stops?
71+
'''
72+
# G(i,j,s) = maximum leftover gas from i-th to j-th station with s stops in-between; 0 <= s <= j-i-1
73+
# G(i,j,0) = gas[i] - sum(cost[i..j-1])
74+
# G(i,j,s) = max{G(i,mid,s1) + G(mid,j,s2) for
75+
# 0 <= s1 <= mid-i-1,
76+
# 0 <= s2 <= j-mid-1,
77+
# s1+s2+1 == s}
78+
# Final results: argmin_s G(i,j,s) >= 0.
79+
# ~O(n^4)
80+
def gas_station3(gas, cost):
81+
n = len(gas)
82+
G = {}
83+
for i in range(n): G[i,(i+1)%n,0] = gas[i] - cost[i]
84+
85+
for k in range(2, n+1):
86+
for i in range(n):
87+
j = i+k
88+
G[i,j%n,0] = gas[i] - sum([cost[x%n] for x in range(i,j)])
89+
for s in range(1, k):
90+
G[i,j%n,s] = -float('inf')
91+
for mid in range(i+1,i+k):
92+
for s1 in range(0, mid-i):
93+
s2 = s-s1-1
94+
if s2 < 0 or s2 > j-mid-1: continue
95+
96+
print '(i,j,s)=(%d,%d,%d)'%(i,j,s), s1, s2
97+
G[i,j%n,s] = max(G[i,j%n,s], \
98+
G[i,mid%n,s1]+G[mid%n,j%n,s2])
99+
#'''
100+
for x,y,z in G:
101+
if z == s:
102+
print x,y,z, ':', G[x,y,z]
103+
#'''
104+
105+
for k,v in G.items(): print k,v
106+
107+
min_step = n-1
108+
for i in range(n):
109+
args = [x for x in range(0,n) if G[i,i,x] > 0]
110+
if args:
111+
min_s = min(args)
112+
min_step = min(min_step, min_s)
113+
print i,'-->',i, 'steps:',min_s, 'leftover gas:', G[i,i,min_s]
114+
return min_step
115+
116+
117+
# Greedy algorithm, starting at
118+
119+
48120
if __name__ == '__main__':
49121
gas = [10,5,10,5,10]
50-
cost = [8,2,2,12,10]
51-
gas_station(gas, cost)
122+
cost = [2,2,2,5,2]
123+
print gas_station3(gas, cost)
52124

53125

n_queens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def check_conflict(cols, n, i, j):
5858

5959

6060
# cols = [col_0, col_1, ..., col_n-1]
61-
# Queen is put on row i, col prevs[i]
61+
# Queen is put on row i, col[i]
6262
# Currently there are len(cols) row
6363
def n_queens_place(cols, n, rets):
6464
if len(cols) == n:

next_permutation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
# 1,5,3,4,2 (find 3,4)
2020
# 2,5,4,3,2 (switch 3,4)
2121
# Example 2:
22-
# 1,5,4,3,2,1,0 (find 1,5; the first element < the left element.)
23-
# 1,5,4,3,2,1,0 (find 2, the first element that > 1 from the end)
22+
# 1,5,4,3,2,1,0 (find 1; the first element (1) < the right element (5).)
23+
# 1,5,4,3,2,1,0 (find 2, the first element > 1 from the end)
2424
# 2,5,4,3,1,1,0 (switch 1,2)
2525
# 2,0,1,1,3,4,5 (reverse 5,4,3,1,1,0)
2626
def next_permu(L):

palin_partition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def palin_partition_min_cut(s):
6262
min_cut = n-i
6363
min_p = None
6464
for p in palin_partition_min_cut(s[i+1:]):
65-
if len(p) < min:
65+
if len(p) < min_cut:
6666
min_cut = len(p)
6767
min_p = p
6868
yield [first] + min_p

permutation_seq.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
'''
1616
from __future__ import division
1717
import random
18+
#from math import factorial
1819

1920
# k = 0,1,...,(n!-1)
2021
def permu_seq(n, k):
21-
from math import factorial
22-
if k >= factorial(n): return ""
22+
fact = 1
23+
for i in range(1,n+1):
24+
fact *= i
25+
if k >= fact: return ""
26+
2327
digits = range(1,n+1)
2428
seq = []
25-
for _ in range(n):
26-
i, k = divmod(k, factorial(n-1))
29+
while n > 0:
30+
fact = int(fact/n)
31+
i, k = divmod(k, fact)
2732
seq.append( digits[i] )
2833
digits.pop(i)
2934
n -= 1

search_2D_matrix.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
For example,
88
Consider the following matrix:
99
[
10-
[1, 3, 5, 7],
10+
[ 1, 3, 5, 7],
1111
[10, 11, 16, 20],
1212
[23, 30, 34, 50]
1313
]
@@ -62,14 +62,15 @@ def search_in_one_quad_matrix(M, val, i, j):
6262

6363
mid_i = (i[0]+i[1]+1)//2
6464
mid_j = (j[0]+j[1]+1)//2
65+
founded = False
6566
for a,b,c,d in [(i[0],j[0],mid_i-1,mid_j-1), \
6667
(i[0],mid_j,mid_i-1,j[1]), \
6768
(mid_i,j[0],i[1],mid_j-1), \
6869
(mid_i,mid_j,i[1],j[1])]:
6970
if M[a][b] <= val <= M[c][d]:
7071
#print 'search_in_one_quad_matrix(M, val', '(',a,c,')', '(',b,d,') )'
71-
return search_in_one_quad_matrix(M, val, (a,c), (b,d))
72-
return False
72+
founded |= search_in_one_quad_matrix(M, val, (a,c), (b,d))
73+
return founded
7374

7475

7576
# Solution#3 Search in 1/4 sub-matrix

search_range.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import division
1212
import random
1313

14+
1415
# O(log n) in time.
1516
# Search in L[i:j]
1617
def binary_range_search(L, val, i, j):

search_rotated_sorted_arr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def search_rotated_arr(L, val):
2121
l = 0; r = len(L)-1
2222
while l <= r:
2323
mid = (l + r + 1)//2
24-
if L[mid] == val: return
24+
if L[mid] == val: return mid
2525
if L[l] <= L[mid]:
2626
# the lower half is sorted
2727
if L[l] <= val <= L[mid]: r = mid-1

0 commit comments

Comments
 (0)