Skip to content

Commit

Permalink
1105-06/06
Browse files Browse the repository at this point in the history
  • Loading branch information
youhusky committed Nov 6, 2017
1 parent 8d401d9 commit 22f00c1
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 1 deletion.
31 changes: 30 additions & 1 deletion 015. 3Sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,33 @@ def threeSum(self, nums):
r -= 1
l += 1
r -= 1
return res
return res

def threeSum2(self, nums):
def hashing(string):
minvalue = min(string)
maxvalue = max(string)
return str(minvalue)+str(maxvalue)
res = []
helper = set()
# wit dup
# res = []
if len(nums) < 3:
return res
for i in range(len(nums)):
dic = {}
for j in range(i+1,len(nums)):
dic[nums[j]] = j
if -nums[i]-nums[j] in dic:
temp = (nums[i], nums[dic[- nums[i]-nums[j]]], nums[j])
# IMP
if hashing(temp) not in helper:
helper.add(hashing(temp))
res.append(temp)

return res


test = [-1, 0, 1, 2, -1, -4]
m = Solution()
print m.threeSum2(test)
26 changes: 26 additions & 0 deletions 075. Sort Colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,29 @@ def sortColors(self, nums):
blue -= 1
else:
i += 1

def sortCate(self, nums):
def cate(n):
if n < 3:
return 'low'
elif n < 6:
return 'medium'
else:
return 'high'
low, high = 0, len(nums)-1
i = 0
while i <= high:
if cate(nums[i]) == 'low':
nums[i], nums[low] = nums[low], nums[i]
i += 1
low += 1
elif cate(nums[i]) == 'high':
nums[i], nums[high] = nums[high], nums[i]
high -= 1
else:
i += 1

m = Solution()
test = [5,7,1,7,5,3,1,8,9,1]
m.sortCate(test)
print test
60 changes: 60 additions & 0 deletions 207. Course Schedule.py
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

59 changes: 59 additions & 0 deletions 208. Course Scheduleii.py
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
98 changes: 98 additions & 0 deletions 269. Alien Dictionary.py
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)


9 changes: 9 additions & 0 deletions README.md
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

0 comments on commit 22f00c1

Please sign in to comment.