add more algorithms · wxpython/algorithms@2fac908 · GitHub
Skip to content

Commit 2fac908

Browse files
committed
add more algorithms
1 parent 24db4c4 commit 2fac908

7 files changed

Lines changed: 127 additions & 28 deletions

File tree

array/merge_intervals.py

Lines changed: 40 additions & 0 deletions

divide-and-conquer/expression_add_operators.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ def add_operator(num, target):
2323
return res
2424

2525
def helper(res, path, num, target, pos, prev, multed):
26-
print(res, path, num, target, pos, prev, multed)
2726
if pos == len(num):
2827
if (target == prev):
2928
res.append(path)
3029
return
3130
for i in range(pos, len(num)):
32-
if i != pos and num[pos] == '0': break
31+
if i != pos and num[pos] == '0': # all digits have to be used
32+
break
3333
cur = int(num[pos:i+1])
34-
print(cur)
35-
if (pos == 0):
34+
if pos == 0:
3635
helper(res, path + str(cur), num, target, i+1, cur, cur)
3736
else:
3837
helper(res, path + "+" + str(cur), num, target, i+1, prev + cur, cur)
@@ -44,4 +43,11 @@ def helper(res, path, num, target, pos, prev, multed):
4443
s = "123"
4544
target = 6
4645
print(add_operator(s, target))
46+
# "232", 8 -> ["2*3+2", "2+3*2"]
47+
s = "232"
48+
target = 8
49+
print(add_operator(s, target))
4750

51+
s = "123045"
52+
target = 3
53+
print(add_operator(s, target))

heap/merge_sorted_k_lists.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
3+
"""
4+
5+
# Definition for singly-linked list.
6+
# class ListNode(object):
7+
# def __init__(self, x):
8+
# self.val = x
9+
# self.next = None
10+
11+
from heapq import heappush, heappop, heapreplace, heapify
12+
13+
def mergeKLists(lists):
14+
dummy = node = ListNode(0)
15+
h = [(n.val, n) for n in lists if n]
16+
heapify(h)
17+
while h:
18+
v, n = h[0]
19+
if n.next is None:
20+
heappop(h) #only change heap size when necessary
21+
else:
22+
heapreplace(h, (n.next.val, n.next))
23+
node.next = n
24+
node = node.next
25+
26+
return dummy.next
27+
28+
from Queue import PriorityQueue
29+
30+
def merge_k_lists(lists):
31+
dummy = ListNode(None)
32+
curr = dummy
33+
q = PriorityQueue()
34+
for node in lists:
35+
if node: q.put((node.val,node))
36+
while q.qsize()>0:
37+
curr.next = q.get()[1]
38+
curr=curr.next
39+
if curr.next: q.put((curr.next.val, curr.next))
40+
return dummy.next
41+
42+
43+
"""
44+
I think my code's complexity is also O(nlogk) and not using heap or priority queue,
45+
n means the total elements and k means the size of list.
46+
47+
The mergeTwoLists functiony in my code comes from the problem Merge Two Sorted Lists
48+
whose complexity obviously is O(n), n is the sum of length of l1 and l2.
49+
50+
To put it simpler, assume the k is 2^x, So the progress of combination is like a full binary tree,
51+
from bottom to top. So on every level of tree, the combination complexity is n,
52+
beacause every level have all n numbers without repetition.
53+
The level of tree is x, ie logk. So the complexity is O(nlogk).
54+
55+
for example, 8 ListNode, and the length of every ListNode is x1, x2,
56+
x3, x4, x5, x6, x7, x8, total is n.
57+
58+
on level 3: x1+x2, x3+x4, x5+x6, x7+x8 sum: n
59+
60+
on level 2: x1+x2+x3+x4, x5+x6+x7+x8 sum: n
61+
62+
on level 1: x1+x2+x3+x4+x5+x6+x7+x8 sum: n
63+
"""

tests/test_stack.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

tree/binary_tree_paths.py

Lines changed: 14 additions & 0 deletions

0 commit comments

Comments
 (0)