From 22f00c1ba65de533b0cefe9f621c4908f5b46b67 Mon Sep 17 00:00:00 2001 From: youhusky Date: Sun, 5 Nov 2017 20:19:27 -0800 Subject: [PATCH] 1105-06/06 --- 015. 3Sum.py | 31 ++++++++++++- 075. Sort Colors.py | 26 +++++++++++ 207. Course Schedule.py | 60 ++++++++++++++++++++++++ 208. Course Scheduleii.py | 59 +++++++++++++++++++++++ 269. Alien Dictionary.py | 98 +++++++++++++++++++++++++++++++++++++++ README.md | 9 ++++ 6 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 207. Course Schedule.py create mode 100644 208. Course Scheduleii.py create mode 100644 269. Alien Dictionary.py diff --git a/015. 3Sum.py b/015. 3Sum.py index e3aa593..d4675d4 100644 --- a/015. 3Sum.py +++ b/015. 3Sum.py @@ -40,4 +40,33 @@ def threeSum(self, nums): r -= 1 l += 1 r -= 1 - return res \ No newline at end of file + 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) diff --git a/075. Sort Colors.py b/075. Sort Colors.py index 814d67a..03089e3 100644 --- a/075. Sort Colors.py +++ b/075. Sort Colors.py @@ -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 diff --git a/207. Course Schedule.py b/207. Course Schedule.py new file mode 100644 index 0000000..dbfcbca --- /dev/null +++ b/207. Course Schedule.py @@ -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 + diff --git a/208. Course Scheduleii.py b/208. Course Scheduleii.py new file mode 100644 index 0000000..f27072a --- /dev/null +++ b/208. Course Scheduleii.py @@ -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 diff --git a/269. Alien Dictionary.py b/269. Alien Dictionary.py new file mode 100644 index 0000000..b6a0ad9 --- /dev/null +++ b/269. Alien Dictionary.py @@ -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) + + \ No newline at end of file diff --git a/README.md b/README.md index c604284..0d9d21c 100644 --- a/README.md +++ b/README.md @@ -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 +