-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw06.py
53 lines (38 loc) · 1005 Bytes
/
hw06.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import heapq
class Queue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
"""
1. Top k Frequent Elements
"""
def kFrequent(nums, k):
freq = Counter(nums)
return heapq.nlargest(k, freq.keys(), key = lambda x:(freq[x], -x))
"""
2. All paths from one vertex to another in DAG
"""
def allPaths(edges, source, destination):
final = []
q = Queue()
q.enqueue([source])
while not q.isEmpty():
path = q.dequeue()
if path[-1] == destination:
final.append(path)
else:
for i in edges:
if i[0] == path[-1]:
q.enqueue(path + [i[1]])
return final
edges = [("a","b"), ("a","c"), ("b","d"), ("c","d")]
source = "a"
destination = "d"
allPaths(edges, source, destination)