-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
282 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# There are a total of n courses you have to take, labeled from 0 to n - 1. | ||
|
||
# Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] | ||
|
||
# Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses? | ||
|
||
# For example: | ||
|
||
# 2, [[1,0]] | ||
# There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. | ||
|
||
# 2, [[1,0],[0,1]] | ||
# There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. | ||
|
||
# Note: | ||
# The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. | ||
# You may assume that there are no duplicate edges in the input prerequisites. | ||
|
||
class Solution(object): | ||
def canFinish(self, numCourses, prerequisites): | ||
""" | ||
O(V+E) | ||
:type numCourses: int | ||
:type prerequisites: List[List[int]] | ||
:rtype: bool | ||
""" | ||
zero_indegree = [] | ||
indegree = {} | ||
outdegree = {} | ||
|
||
# save the indegree and outdegree | ||
for i, j in prerequisites: | ||
if i not in indegree: | ||
indegree[i] = set() | ||
if j not in outdegree: | ||
outdegree[j] = set() | ||
indegree[i].add(j) | ||
outdegree[j].add(i) | ||
|
||
# find zero indegree in the graph | ||
for i in range(numCourses): | ||
if i not in indegree: | ||
zero_indegree.append(i) | ||
|
||
while zero_indegree: | ||
prerequest = zero_indegree.pop(0) | ||
if prerequest in outdegree: | ||
for course in outdegree[prerequest]: | ||
indegree[course].remove(prerequest) | ||
# empty | ||
if not indegree[course]: | ||
zero_indegree.append(course) | ||
del (outdegree[prerequest]) | ||
|
||
|
||
# check out degree | ||
if outdegree: | ||
return False | ||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# There are a total of n courses you have to take, labeled from 0 to n - 1. | ||
|
||
# Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] | ||
|
||
# Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses. | ||
|
||
# There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array. | ||
|
||
# For example: | ||
|
||
# 2, [[1,0]] | ||
# There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1] | ||
|
||
# 4, [[1,0],[2,0],[3,1],[3,2]] | ||
# There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3]. | ||
|
||
class Solution(object): | ||
def findOrder(self, numCourses, prerequisites): | ||
""" | ||
O(V+E) | ||
O(E) | ||
:type numCourses: int | ||
:type prerequisites: List[List[int]] | ||
:rtype: List[int] | ||
""" | ||
zero_indegree = [] | ||
indegree = {} | ||
outdegree = {} | ||
res = [] | ||
# save the indegree and outdegree | ||
for i, j in prerequisites: | ||
if i not in indegree: | ||
indegree[i] = set() | ||
if j not in outdegree: | ||
outdegree[j] = set() | ||
indegree[i].add(j) | ||
outdegree[j].add(i) | ||
|
||
# find zero indegree in the graph | ||
for i in range(numCourses): | ||
if i not in indegree: | ||
zero_indegree.append(i) | ||
|
||
while zero_indegree: | ||
prerequest = zero_indegree.pop(0) | ||
res.append(prerequest) | ||
if prerequest in outdegree: | ||
for course in outdegree[prerequest]: | ||
indegree[course].remove(prerequest) | ||
# empty | ||
if not indegree[course]: | ||
zero_indegree.append(course) | ||
del (outdegree[prerequest]) | ||
|
||
|
||
# check out degree | ||
if outdegree: | ||
return [] | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language. | ||
|
||
# Example 1: | ||
# Given the following words in dictionary, | ||
|
||
# [ | ||
# "wrt", | ||
# "wrf", | ||
# "er", | ||
# "ett", | ||
# "rftt" | ||
# ] | ||
# The correct order is: "wertf". | ||
|
||
# Example 2: | ||
# Given the following words in dictionary, | ||
|
||
# [ | ||
# "z", | ||
# "x" | ||
# ] | ||
# The correct order is: "zx". | ||
|
||
# Example 3: | ||
# Given the following words in dictionary, | ||
|
||
# [ | ||
# "z", | ||
# "x", | ||
# "z" | ||
# ] | ||
# The order is invalid, so return "". | ||
|
||
# Note: | ||
# You may assume all letters are in lowercase. | ||
# You may assume that if a is a prefix of b, then a must appear before b in the given dictionary. | ||
# If the order is invalid, return an empty string. | ||
# There may be multiple valid order of letters, return any one of them is fine. | ||
|
||
# Top logical + course schedule ii | ||
# build toplogical graph | ||
class Solution(object): | ||
def buildgraph(self, word1, word2, indegree, outdegree): | ||
length = min(len(word1), len(word2)) | ||
for i in range(length): | ||
if word1[i] != word2[i]: | ||
if word2[i] not in indegree: | ||
indegree[word2[i]] = set() | ||
if word1[i] not in outdegree: | ||
outdegree[word1[i]] = set() | ||
indegree[word2[i]].add(word1[i]) | ||
outdegree[word1[i]].add(word2[i]) | ||
break | ||
|
||
def alienOrder(self, words): | ||
""" | ||
:type words: List[str] | ||
:rtype: str | ||
""" | ||
# queue | ||
zero_indegree= [] | ||
indegree = {} | ||
outdegree = {} | ||
res = [] | ||
|
||
# init | ||
nodes = set() | ||
for word in words: | ||
for char in word: | ||
nodes.add(char) | ||
|
||
# corner case | ||
# iterate each word like play and playing | ||
for i in range(1, len(words)): | ||
|
||
if len(words[i-1]) > len(words[i]) and words[i-1][:len(words[i])] == words[i]: | ||
continue | ||
self.buildgraph(words[i-1], words[i],indegree, outdegree) | ||
# zero indegree | ||
for node in nodes: | ||
if node not in indegree: | ||
zero_indegree.append(node) | ||
|
||
# toplogicial | ||
while zero_indegree: | ||
prerequest = zero_indegree.pop(0) | ||
res.append(prerequest) | ||
if prerequest in outdegree: | ||
for j in outdegree[prerequest]: | ||
indegree[j].remove(prerequest) | ||
if not indegree[j]: | ||
zero_indegree.append(j) | ||
del(outdegree[prerequest]) | ||
if outdegree: | ||
return "" | ||
return "".join(res) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
# Facebook_Prepare | ||
|
||
## Freq | ||
|
||
[x] Three sum without sorted | ||
[x] Task Scheduler | ||
[] 301 remove invalid parenthesis | ||
[] 17 letter combination of phone | ||
[x] Sort the numbers based on categories -- 075 follow up | ||
|