Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
216d4af
no message
Aug 20, 2020
c198584
no message
Aug 23, 2020
cd00866
no message
Aug 24, 2020
f05cd51
no message
Aug 24, 2020
8fe6976
no message
Oct 9, 2020
0441347
no message
Oct 13, 2020
67e86e5
no message
Oct 17, 2020
3920ee2
no message
Oct 18, 2020
f96764a
no message
Nov 9, 2020
f3534db
no message
Feb 10, 2021
48e14b0
no message
Feb 19, 2021
14bbaa2
no message
Mar 3, 2021
e18641c
no message
Mar 6, 2021
dc84873
no message
Mar 7, 2021
464fe6b
no message
Apr 10, 2021
a0aaa90
no message
Apr 19, 2021
3fd9514
no message
May 16, 2021
11a12fb
no message
May 16, 2021
4ceb3e1
no message
Jun 2, 2021
c39681e
no message
Jun 7, 2021
b7dfe9c
no message
Jun 19, 2021
39884b7
Update README.
Jun 19, 2021
de66880
no message
Jun 19, 2021
28f997a
no message
Jun 21, 2021
ac0ddec
no message
Jun 24, 2021
7cba347
no message
Jul 3, 2021
9c99683
no message
Jul 9, 2021
4e694d3
no message
Jul 22, 2021
528b143
no message
Jul 26, 2021
dc6fea4
no message
Aug 3, 2021
f682d59
no message
Aug 7, 2021
1642d02
no message
Aug 8, 2021
430b509
no message
Aug 10, 2021
e8ca0c1
no message
Aug 10, 2021
f16e6e9
no message
Aug 12, 2021
42ded99
no message
wuduhren Aug 24, 2021
2d438e1
no message
wuduhren Aug 30, 2021
0c053b4
no message
wuduhren Sep 1, 2021
0c70368
no message
wuduhren Sep 2, 2021
fac3abc
no message
wuduhren Sep 9, 2021
158fa82
no message
wuduhren Sep 9, 2021
ec536c0
no message
wuduhren Sep 10, 2021
c9e8b16
no message
wuduhren Sep 13, 2021
85a1dea
no message
wuduhren Sep 14, 2021
2ee868a
no message
wuduhren Sep 17, 2021
780cc80
no message
wuduhren Sep 19, 2021
4e46d7d
no message
wuduhren Sep 19, 2021
f15efb2
no message
wuduhren Sep 23, 2021
2acab52
no message
wuduhren Sep 25, 2021
5e5e2ff
no message
wuduhren Sep 26, 2021
9f00706
no message
wuduhren Sep 27, 2021
5111543
no message
wuduhren Sep 30, 2021
0637d3a
no message
wuduhren Oct 1, 2021
895710d
no message
wuduhren Oct 3, 2021
c6c84d9
no message
wuduhren Oct 5, 2021
75bd00b
no message
wuduhren Oct 7, 2021
89c0836
no message
wuduhren Oct 12, 2021
b744b87
no message
wuduhren Nov 22, 2021
891b50d
no message
wuduhren Nov 28, 2021
d7ddf40
no message
wuduhren Dec 1, 2021
7e4c13b
no message
wuduhren Dec 6, 2021
a20b61a
no message
wuduhren Dec 11, 2021
6c6b1c0
no message
wuduhren Dec 11, 2021
eca05c7
no message
wuduhren Dec 22, 2021
133c9bc
no message
wuduhren Jan 2, 2022
fd5bc7c
no message
wuduhren Jan 11, 2022
98a3a80
no message
wuduhren Jan 13, 2022
81ec8a5
no message
wuduhren Jan 14, 2022
82cd50e
no message
wuduhren Jan 21, 2022
5e28daf
no message
wuduhren Jan 21, 2022
a2551c9
no message
wuduhren Jan 21, 2022
803fb76
no message
wuduhren Jan 24, 2022
5dda17b
no message
wuduhren Feb 3, 2022
13fbec9
no message
wuduhren Feb 15, 2022
0de2fcf
no message
wuduhren Feb 20, 2022
2191a53
no message
wuduhren Mar 1, 2022
a9671e8
no message
wuduhren Mar 2, 2022
6f00d18
no message
wuduhren Mar 5, 2022
3aedd4c
no message
wuduhren Apr 25, 2022
54dd56f
no message
wuduhren Apr 25, 2022
740f160
no message
wuduhren Apr 25, 2022
b9c2a70
no message
wuduhren Apr 26, 2022
138947d
no message
wuduhren Apr 28, 2022
d63b770
no message
wuduhren May 5, 2022
0e5f71d
no message
wuduhren May 14, 2022
c6ba939
no message
wuduhren May 17, 2022
3856e22
20220530
May 29, 2022
aa833bb
combination-sum
May 31, 2022
ec99f02
permutation
Jun 1, 2022
06dd433
subsets-ii
Jun 2, 2022
048b1c8
combination-sum-ii
Jun 3, 2022
66f6f8d
update
Jun 7, 2022
777337b
updates
Jun 10, 2022
7568868
updates
Jun 16, 2022
b178190
updates
Jun 21, 2022
ab00993
updates
Jul 6, 2022
881c58d
updates
Jul 17, 2022
53d11bc
updates
Jul 23, 2022
b68839f
update
Aug 1, 2022
612e17e
update
Aug 9, 2022
1029696
Updates
Aug 14, 2022
543e1f8
updates
Aug 20, 2022
758ba68
Updates
Aug 25, 2022
91179a3
updates
Sep 2, 2022
dddf785
updates
Sep 2, 2022
5a67c5f
updates
Sep 20, 2022
02d6d3b
update
Sep 25, 2022
3af7236
updates
Sep 25, 2022
79b9b8f
updates
Oct 8, 2022
82ca4d9
updates
Nov 1, 2022
bcef8a4
updates
Nov 27, 2022
f14be0f
updates
Dec 1, 2022
dbaa78d
updates
Dec 5, 2022
733fd02
updates
Dec 5, 2022
74a3446
update
Dec 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file modified .gitignore
100644 → 100755
Empty file.
166 changes: 15 additions & 151 deletions README.md
100644 → 100755

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions common/bellman–ford.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
First, we use `distance` to track the distance from start to all others.
For V nodes, it takes at least V-1 iteration to complete the algorithm.
For every iteration,
We use every node as the middle point to see if it can "loosen" the path from start to mid to mid's neighbors
If it can loosen the path, we update the `distance`.
The time complexity is O(VE)
"""
def min_path(G, N, start, end):
distance = [float('inf') for _ in xrange(N+1)]
distance[start] = 0

for _ in xrange(N-1):
for mid, dis in enumerate(distance):
if dis==float('inf'): continue
for dis_to_nei, nei in G[mid]:
distance[nei] = min(distance[nei], dis+dis_to_nei)
return distance[end]


G = {
0: [(-2, 1), (4, 2)],
1: [(5, 2)],
2: [(12, 3), (5, 4)],
3: [(-8, 4)],
4: []
}
N = 4 #nodes count
start = 0
end = 4
print min_path(G, N, start, end)
97 changes: 97 additions & 0 deletions common/binary-search-tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class Node(object):
def __init__(self, val):
self.left = None
self.right = None
self.val = val


class BinarySearchTree(object):
def __init__(self, val):
self.root = Node(val)

def insert(self, val):
def helper(node, val):
if val>node.val:
if node.right:
helper(node.right, val)
else:
node.right = Node(val)
elif val<node.val:
if node.left:
helper(node.left, val)
else:
node.left = Node(val)
helper(self.root, val)
# self.printTree()

def search(self, val):
def helper(node, val):
if node==None:
return False
elif val==node.val:
return True
elif val>node.val:
return helper(node.right, val)
elif val<node.val:
return helper(node.left, val)

print helper(self.root, val)
# self.printTree()

def inOrderPrint(self):
def helper(node):
if node.left: helper(node.left)
print node.val
if node.right: helper(node.right)
helper(self.root)

def preOrderPrint(self):
def helper(node):
print node.val
if node.left: helper(node.left)
if node.right: helper(node.right)
helper(self.root)

def postOrderPrint(self):
def helper(node):
if node.left: helper(node.left)
if node.right: helper(node.right)
print node.val
helper(self.root)

tree = BinarySearchTree(4)
tree.insert(2)
tree.insert(1)
tree.insert(3)
tree.insert(5)
tree.insert(5)
tree.insert(7)

tree.inOrderPrint()
print '====='
tree.preOrderPrint()
print '====='
tree.postOrderPrint()
print '====='

tree.search(3)
tree.search(10)



















15 changes: 15 additions & 0 deletions common/bst-in-order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def inOrderTraverse(root):
stack = []
node = root

while node or stack:
while node:
stack.append(node)
node = node.left

node = stack.pop()

#do something
print node.val

node = node.right
87 changes: 87 additions & 0 deletions common/dijkstra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import heapq

"""
If we only want to find the shortest distance to the end and not all the nodes's shortest distance
The ending condition will be when `end` pop out from the `pq`.
And the path would be all the nodes that were popped out.
"""

#heap implementation
def min_path(G, start, end):
distance = {}
pq = []
prev = {}
path_str = ''
visited = set()

distance[start] = 0
heapq.heappush(pq, (0, start))

while pq:
dis_to_mid, mid = heapq.heappop(pq)
visited.add(mid)

for dis, nei in G[mid]:
if nei not in distance or distance[nei]>dis_to_mid+dis:
distance[nei] = dis_to_mid+dis
prev[nei] = mid
if nei not in visited:
heapq.heappush(pq, (distance[nei], nei))

curr = end
while True:
if curr not in prev: break
path_str = ' -> '+prev[curr]+path_str
curr = prev[curr]

return path_str+' = '+str(distance[end])



#normal implementation
def min_path(G, start, end):
distance = {} #shortest distance from start
prev = {}
path_str = ''
visited = set()

distance[start] = 0
while True:
#find nearest unvisited node
mid = None
dis_to_mid = float('inf')
for node in distance:
if node not in visited and distance[node]<dis_to_mid:
mid = node
dis_to_mid = distance[node]

if mid==None: break
visited.add(mid)

#try to use mid to loosen the prev from start to mid's neighbors
for dis, nei in G[mid]:
if nei not in distance or dis_to_mid+dis<distance[nei]:
distance[nei] = dis_to_mid+dis
prev[nei] = mid

curr = end
while True:
if curr not in prev: break
path_str = ' -> '+prev[curr]+path_str
curr = prev[curr]

print path_str+' = '+str(distance[end])

G = {
'0': [(1, '1'), (12, '2')],
'1': [(9, '2'), (3, '3')],
'2': [(5, '4')],
'3': [(4, '2'), (13, '4'), (15, '5')],
'4': [(4, '5')],
'5': []
}

print min_path(G, '0', '5')



33 changes: 33 additions & 0 deletions common/heap-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#O(LogN), used when part of the array is already heapified.
def heapify(A, N, i):
largest = i
l = i*2+1
r = i*2+2

if l<N and A[l]>A[largest]: largest = l
if r<N and A[r]>A[largest]: largest = r
if largest!=i:
A[largest], A[i] = A[i], A[largest]
heapify(A, N, largest)

#O(NLogN)
def heapSort(A):
N = len(A)

#build max heap, O(NLogN). Can be optimized to the O(N).
for i in range(N//2-1, -1, -1):
heapify(A, N, i)

#keep swapping the largest
for i in range(N-1, -1, -1):
A[0], A[i] = A[i], A[0]
heapify(A, i, 0)


A = [12, 11, 13, 5, 6, 7]
heapSort(A)
print(A)

A = [1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17]
heapSort(A)
print(A)
19 changes: 19 additions & 0 deletions common/insertion-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def insertionSort(nums):
for i in xrange(len(nums)):
num = nums[i]
j = i-1
while j>=0 and num<nums[j]:
nums[j+1] = nums[j]
j-=1
nums[j+1] = num
return nums


if __name__ == '__main__':
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]
print insertionSort(test)

"""
Time Complexity is O(N^2) in average and worst case. O(N) in best case.
Space Complexity is O(1).
"""
42 changes: 42 additions & 0 deletions common/knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def get_max_value(value, weight, max_weight):
N = len(value)
K = [[0 for _ in xrange(max_weight+1)] for _ in xrange(N+1)]
bag = []

for i in xrange(N+1):
for mw in xrange(max_weight+1):
if i==0 or mw==0:
K[i][mw] = 0
continue
w = weight[i-1]
v = value[i-1]
if w>mw:
K[i][mw] = K[i-1][mw]
else:
K[i][mw] = max(v+K[i-1][mw-w], K[i-1][mw])

max_value = K[-1][-1]

w = max_weight
i = N
while i>0:
if K[i][w]-K[i-1][w]>0:
bag.append(i-1)
w = w - weight[i-1]
i = i-1

print 'bag: ', bag
print 'max_value', max_value
return max_value


value = [60, 100, 120]
weight = [10, 20, 30]
max_weight = 50
get_max_value(value, weight, max_weight)

value = [40, 100, 50, 60]
weight = [20, 10, 40, 30]
max_weight = 60
get_max_value(value, weight, max_weight)

22 changes: 22 additions & 0 deletions common/merge-sort-in-place.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def merge(A, start, mid, end):
L, R = A[start:mid], A[mid:end]
i = j = 0
k = start
for l in xrange(k,end):
if j>=len(R) or (i<len(L) and L[i]<R[j]):
A[l] = L[i]
i = i+1
else:
A[l] = R[j]
j = j+1

def mergeSort(A, p, r):
if r - p > 1:
mid = (p+r)/2
mergeSort(A, p, mid)
mergeSort(A, mid, r)
merge(A, p, mid, r)

A = [20, 30, 21, 15, 42, 45, 31, 0, 9]
mergeSort(A, 0, len(A))
print A
Loading