classSolution(object): deffindOrder(self, numCourses, prerequisites): """ :type numCourses: int :type prerequisites: List[List[int]] :rtype: List[int] """ outdegree = [[] for _ in range(numCourses)] indegree = [0] * numCourses
for succ, pre in prerequisites: outdegree[pre].append(succ) indegree[succ] += 1
queue = [] for i in range(numCourses): if indegree[i] == 0: queue.append(i)
res = []
while queue: pre = queue.pop(0) res.append(pre) for succ in outdegree[pre]: indegree[succ] -= 1 if indegree[succ] == 0: queue.append(succ) return res if len(res) == numCourses else []
for i in range(len(graph)): outdegree[i] = len(graph[i]) for j in range(len(graph[i])): indegree[graph[i][j]].append(i)
queue = [] for i in range(len(outdegree)): if outdegree[i] == 0: queue.append(i) res = [] while queue: node = queue.pop(0) res.append(node) if indegree[node]: for rest in indegree[node]: outdegree[rest] -= 1 if outdegree[rest] == 0: queue.append(rest)
for seq in seqs: # union set st |= set(seq) if len(seq) == 1: if seq[0] notin indegree: indegree[seq[0]] = 0 continue for i in range(len(seq)-1): if seq[i] notin indegree: indegree[seq[i]] = 0 if seq[i+1] notin outdegree[seq[i]]: outdegree[seq[i]].append(seq[i+1]) indegree[seq[i+1]] += 1
zero_degree = 0 queue = []
for each in indegree: if indegree[each] == 0: queue.append(each) zero_degree += 1 # unique if zero_degree > 1: returnFalse
res = []
while queue: prev = queue.pop(0) res.append(prev) count = 0 for succ in outdegree[prev]: indegree[succ] -= 1 if indegree[succ] == 0: count += 1 queue.append(succ) # not unique if count > 1: returnFalse # if left if outdegree[prev] andnot count: returnFalse return res == org and set(org) == set(st)
269. Alien Dictionary
Hard 难度,一方面是构建dictionary的时候很繁琐. 每次判断完后要del掉outdegree所对应pop出来的元素,直到没有出度,也就是全部遍历完了才成功。因为order的长度没有给出,所以不能用len(order) == len(origin) 来判断
for i in range(1, len(words)): # consider play and playing if len(words[i-1]) > len(words[i]) and words[i-1][:len(words[i])] == words[i]: continue self.buildToplogicalSort(words[i-1], words[i], indegree, outdegree)
# Take care some corner cases (newly added) ifnot outdegree and len(words) == 2and len(words[0]) > len(words[1]): return''
# build number of char nodes = set() for word in words: for char in word: nodes.add(char)
for char in nodes: if indegree[char] == 0: #if char not in indegree: queue.append(char)
while queue: prev = queue.pop(0) res.append(prev) # we need to check outdegree because we del outdegree if we find
for succ in outdegree[prev]: indegree[succ] -= 1 if indegree[succ] == 0: queue.append(succ) # del outdegree for this char del(outdegree[prev])
if outdegree: return"" return"".join(res)
defbuildToplogicalSort(self, word1, word2, indegree, outdegree): length = min(len(word1), len(word2)) for i in range(length): if word1[i] != word2[i]: # init pre char # if word1[i] not in outdegree: # outdegree[word1[i]] = set() if word2[i] notin outdegree[word1[i]]: indegree[word2[i]] += 1 outdegree[word1[i]].append(word2[i]) # only contain two char is not the same its order after that is irrelevent break