Grokking Graph Algorithms for Coding Interviews
0% completed
All Tasks Scheduling Orders (hard)
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
The official solution is bad and disagree with how it was done. how uses queue.remove() ???. that's O(operation)
This is a the proper solution to the question
from collections import deque class Solution: def __init__(self): self.orders = [] # this is a backtracking / permutatinal + topological solution def topo(self, path, seen): # base case if len(path) == len(self.graph): self.orders.append(path[:]) return # instead of using queue to procee the next 0 node we recurse and loop to find the 0 node for node in self.graph: if self.indegree[node] == 0 and not seen.get(node): seen[node] = True path.append(node) for nex_node in self.graph[node]: self.indegree[nex_node] -= 1 self.to
A
Anonymous
· 4 years ago
Is the "if (!sources.isEmpty()) ..." line redundant, considering we're looping on sources anyway?
Reading Progress
0%