Skip to content

Commit 585debd

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent 7bd382c commit 585debd

21 files changed

+283
-45
lines changed

3sum.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def threeSum(self, nums):
4949
l+=1
5050
r-=1
5151
return res
52+
53+
"""
54+
I really take time to make the best solution, because I wanted to help people understand.
55+
If you like my answer, a star on GitHub I will really appreciated.
56+
https://github.com/wuduhren/leetcode-
57+
"""
5258

5359
"""
5460
def threeSum(self, nums):

add-binary.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#https://leetcode.com/problems/add-binary/
22
class Solution(object):
33
def addBinary(self, a, b):
4-
return bin(int(a, 2)+int(b, 2))[2:]
4+
return bin(int(a, 2)+int(b, 2))[2:]
5+
6+
"""
7+
I really take time to make the best solution, because I wanted to help people understand.
8+
If you like my answer, a star on GitHub I will really appreciated.
9+
https://github.com/wuduhren/leetcode-python
10+
"""

best-time-to-buy-an-stock.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ def maxProfit(self, prices):
1111
minPrice = price
1212
if (price-minPrice>maxProfit):
1313
maxProfit = price-minPrice
14-
return maxProfit
14+
return maxProfit
15+
16+
"""
17+
I really take time to make the best solution, because I wanted to help people understand.
18+
If you like my answer, a star on GitHub I will really appreciated.
19+
https://github.com/wuduhren/leetcode-python
20+
"""

binary-search-tree-iterator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class BSTIterator(object):
9+
10+
def __init__(self, root):
11+
self.stack = []
12+
while root:
13+
self.stack.append(root)
14+
root = root.left
15+
16+
17+
18+
def next(self):
19+
node = self.stack.pop()
20+
r = node.right
21+
while r:
22+
self.stack.append(r)
23+
r = r.left
24+
return node.val
25+
26+
27+
28+
def hasNext(self):
29+
return len(self.stack)!=0
30+
31+
32+
33+
# Your BSTIterator object will be instantiated and called as such:
34+
# obj = BSTIterator(root)
35+
# param_1 = obj.next()
36+
# param_2 = obj.hasNext()
37+
38+
"""
39+
I really take time to make the best solution, because I wanted to help people understand.
40+
If you like my answer, a star on GitHub I will really appreciated.
41+
https://github.com/wuduhren/leetcode-python
42+
"""

course-schedule.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
we build a graph of adjacency list, like [0]
3+
0->[2,4,5]
4+
1->[3,4]
5+
meaning before taking 2,4,5 we need to take 0, before taking 3,4 we need to take 1
6+
if we find a loop back to itself then it is impossible, for example
7+
0->[2,4,5]
8+
1->[3,4]
9+
2->[0,3]
10+
0->2->0, which is imposible
11+
12+
now we iterate every course to see if it can loop back to itself in anyway [1]
13+
we do this by dfs and search for it self
14+
if we find itself we find loop
15+
16+
the time efficiency is O(V^2+VE), bc each dfs in adjacency list is O(V+E) and we do it V times
17+
space efficiency is O(E)
18+
V is the numCourses(Vertices)
19+
E is the prerequisites(Edges)
20+
"""
21+
class Solution(object):
22+
def canFinish(self, numCourses, prerequisites):
23+
graph = {n:[] for n in xrange(numCourses)} #[0]
24+
25+
for n1, n2 in prerequisites:
26+
graph[n1].append(n2)
27+
28+
for target_course in xrange(numCourses): #[1]
29+
stack = graph[target_course]
30+
visited = set()
31+
while stack:
32+
course = stack.pop()
33+
visited.add(course)
34+
if course==target_course: return False
35+
for ajc in graph[course]:
36+
if ajc not in visited:
37+
stack.append(ajc)
38+
return True

first-unique-character-in-a-string.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ def firstUniqChar(self, string):
1414
if counter[char]==1:
1515
return i
1616

17-
return -1
17+
return -1
18+
19+
"""
20+
I really take time to make the best solution, because I wanted to help people understand.
21+
If you like my answer, a star on GitHub I will really appreciated.
22+
https://github.com/wuduhren/leetcode-python
23+
"""

group-anagrams.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ def groupAnagrams(self, strs):
1111

1212
dic[tuple(string_count)].append(string)
1313

14-
return dic.values()
14+
return dic.values()
15+
16+
"""
17+
I really take time to make the best solution, because I wanted to help people understand.
18+
If you like my answer, a star on GitHub I will really appreciated.
19+
https://github.com/wuduhren/leetcode-
20+
"""

linked-list-cycle.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ def hasCycle(self, head):
88
slow = slow.next
99
if (fast==slow):
1010
return True
11-
return False
11+
return False
12+
13+
"""
14+
I really take time to make the best solution, because I wanted to help people understand.
15+
If you like my answer, a star on GitHub I will really appreciated.
16+
https://github.com/wuduhren/leetcode-python
17+
"""

longest-common-prefix.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ def longestCommonPrefix(self, strs):
1414
return ''
1515
else:
1616
return bench_mark[:i-1]
17-
return bench_mark
17+
return bench_mark
18+
19+
"""
20+
I really take time to make the best solution, because I wanted to help people understand.
21+
If you like my answer, a star on GitHub I will really appreciated.
22+
https://github.com/wuduhren/leetcode-python
23+
"""

longest-palindromic-substring.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
"""
2+
findPalindorome() find the palindrone string from the center "mid"
3+
The center could be one char (case 1) or two the same char (case 2)
4+
We iterate through every char in string and assume it is the center
5+
And put it in findPalindorome() to find the longest
6+
7+
Time Efficiency: O(N^2)
8+
Space Efficiency: O(N)
9+
"""
110
class Solution(object):
211
def longestPalindrome(self, s):
3-
4-
"""
5-
findPalindorome() find the palindrone string from the center "mid"
6-
The center could be one char (case 1) or two the same char (case 2)
7-
We iterate through every char in string and assume it is the center
8-
And put it in findPalindorome() to find the longest
9-
10-
Time Efficiency: O(N^2)
11-
Space Efficiency: O(N)
12-
"""
1312
def findPalindorome(mid, mid2=None):
1413
r = 0
1514
l = len(s)
@@ -42,5 +41,9 @@ def findPalindorome(mid, mid2=None):
4241
max_pal = p2 if len(p2)>len(max_pal) else max_pal
4342

4443
return max_pal
45-
44+
"""
45+
I really take time to explain my solution, because I wanted to help people understand.
46+
If you like my answer, a star on GitHub I will really appreciated.
47+
https://github.com/wuduhren/leetcode-python
48+
"""
4649

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
class Solution(object):
1+
"""
2+
everytime we iterate to the next char (on index i)
3+
we move the start to the place, where all the char between the start and i are all unique
4+
and keep undating counter til the end
25
3-
"""
4-
everytime we iterate to the next char (on index i)
5-
we move the start to the place, where all the char between the start and i are all unique
6-
and keep undating counter til the end
7-
8-
how do we move the start?
9-
we write down the index of the char we last seen
10-
if char is seen, we set the start to (the index we seen char last time)+1
11-
so we don't repeat char, BUT
6+
how do we move the start?
7+
we write down the index of the char we last seen
8+
if char is seen, we set the start to (the index we seen char last time)+1
9+
so we don't repeat char, BUT
1210
13-
another rule of start is not moving leftward
14-
bc this will cause more repeating char between start and i
15-
so we have to keep start incremental
16-
17-
we only iterate through the string once so
18-
Time Efficiency is O(N)
11+
another rule of start is not moving leftward
12+
bc this will cause more repeating char between start and i
13+
so we have to keep start incremental
1914
20-
we use a dict to keep track of the index of char so
21-
Space Efficiency is O(N)
22-
"""
15+
we only iterate through the string once so
16+
Time Efficiency is O(N)
2317
18+
we use a dict to keep track of the index of char so
19+
Space Efficiency is O(N)
20+
"""
21+
class Solution(object):
2422
def lengthOfLongestSubstring(self, s):
2523
if s is None or s=='': return 0
2624
counter = 0 #max length of substring
@@ -34,4 +32,9 @@ def lengthOfLongestSubstring(self, s):
3432
counter = max(counter, i-start+1)
3533
mark[char_now] = i
3634

37-
return counter
35+
return counter
36+
"""
37+
I really take time to explain my solution, because I wanted to help people understand.
38+
If you like my answer, a star on GitHub I will really appreciated.
39+
https://github.com/wuduhren/leetcode-python
40+
"""

meeting-rooms-ii.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ def minMeetingRooms(self, intervals):
5050

5151
return len(free_rooms)
5252

53-
53+
"""
54+
I really take time to make the best solution, because I wanted to help people understand.
55+
If you like my answer, a star on GitHub I will really appreciated.
56+
https://github.com/wuduhren/leetcode-python
57+
"""
5458

5559

5660

merge-two-sorted-lists.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ def mergeTwoLists(self, l1, l2):
2525
elif l2:
2626
curr.next = l2
2727

28-
return pre.next
28+
return pre.next
29+
30+
"""
31+
I really take time to make the best solution, because I wanted to help people understand.
32+
If you like my answer, a star on GitHub I will really appreciated.
33+
https://github.com/wuduhren/leetcode-python
34+
"""

min-stack.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ def getMin(self):
3030
# obj.push(x)
3131
# obj.pop()
3232
# param_3 = obj.top()
33-
# param_4 = obj.getMin()
33+
# param_4 = obj.getMin()
34+
35+
"""
36+
I really take time to make the best solution, because I wanted to help people understand.
37+
If you like my answer, a star on GitHub I will really appreciated.
38+
https://github.com/wuduhren/leetcode-python
39+
"""

moving-average-from-data-stream.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ def next(self, val):
1313
else:
1414
self.sum = self.sum+val
1515

16-
return self.sum/float(len(self.queue))
16+
return self.sum/float(len(self.queue))
17+
18+
"""
19+
I really take time to make the best solution, because I wanted to help people understand.
20+
If you like my answer, a star on GitHub I will really appreciated.
21+
https://github.com/wuduhren/leetcode-python
22+
"""

network-delay-time.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Dijkstra's algorithm heap implementation
3+
first, we build an adjacent list. [0]
4+
if you don't know what is adjacent list, see https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs
5+
6+
we use a hash-map 'dis' to keep track of every node's distance to K. [1]
7+
and we use a priority queue 'pq' to store all the node we encounter and its distance to K. [2]
8+
which we use a tuple (distance to K, node)
9+
10+
for every node we visit, if its distance to K is determined, we don't need to look at it anymore. [3]
11+
because we always pop the nearest one to the K in the priority queue, we can be sure that the distance in 'dis' is the shortest.
12+
then from the node, which we know the shortest path from K to the node, we keep on explore its neighbors. [4]
13+
14+
then if we didn't visit every node we return -1, else we return the node which it takes the longest time to reach. [5]
15+
16+
for time complexity
17+
we use O(E) to make an adjacency list from edge list.
18+
we will go through the while loop V-1 times because we need to determined the distance of V-1 other nodes.
19+
for every loop
20+
we need O(logV) to get the nearest node, bc we pop from priority queue.
21+
we go push the node the priority queue d times (which is the depth of every node)
22+
push the node takes O(logV), we assume all the node is in the queue.
23+
so every loop we use O(d*logV+logV)~=O(d*logV)
24+
so total for total, it is O((V-1)*(d*logV))+O(E), we know that V-1~=V and V*d=E
25+
so it is O(ElogV)+O(E)~=O(ElogV)
26+
E is the number of edges, which is the length of 'times' of the input
27+
V is the number of node, which is the 'N' of the inout
28+
29+
for space complexity
30+
we used O(V) in both the 'dis' and 'pq', and O(E) on the adjacency list.
31+
so, it is O(V+E).
32+
"""
33+
class Solution(object):
34+
def networkDelayTime(self, times, N, K):
35+
aj_list = collections.defaultdict(list) #[0]
36+
for u, v, w in times:
37+
aj_list[u].append((w, v))
38+
39+
dis = {} #[1]
40+
pq = [(0, K)] #[2]
41+
42+
while pq:
43+
if len(dis)==N: break
44+
45+
d, node = heapq.heappop(pq)
46+
if node in dis: continue #[3]
47+
48+
dis[node] = d
49+
50+
for d2, nb in aj_list[node]: #[4]
51+
if nb not in dis: #[3]
52+
heapq.heappush(pq, (d+d2, nb)) #[2]
53+
54+
return max(dis.values()) if len(dis)==N else -1 #[5]
55+
"""
56+
I really take some time to explain my solution, because I wanted to help people understand.
57+
If you like my answer, a star on GitHub I will really appreciated.
58+
https://github.com/wuduhren/leetcode-python
59+
"""

0 commit comments

Comments
 (0)