Skip to content

Commit 1642d02

Browse files
author
Chris Wu
committed
no message
1 parent f682d59 commit 1642d02

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Time: O(N)
3+
Space: O(1)
4+
5+
Keep updating the minimum element (`min1`) and the second minimum element (`min2`).
6+
When a new element comes up there are 3 possibilities.
7+
0. Equals to min1 or min2 => do nothing.
8+
1. Smaller than min1 => update min1.
9+
2. Larger than min1 and smaller than min2 => update min2.
10+
3. Larger than min2 => return True.
11+
"""
12+
class Solution(object):
13+
def increasingTriplet(self, nums):
14+
min1 = min2 = float('inf')
15+
16+
for n in nums:
17+
if n<min1:
18+
min1 = n
19+
elif min1<n and n<min2:
20+
min2 = n
21+
elif min2<n:
22+
return True
23+
24+
return False
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Sort
3+
Time: O(NLogN)
4+
Space: O(1)
5+
"""
6+
class Solution(object):
7+
def findKthLargest(self, nums, k):
8+
nums.sort()
9+
return nums[-k]
10+
11+
"""
12+
Heap
13+
Time: O(N+KLogN)
14+
Space: O(N)
15+
16+
Python heapq is implemented as a min heap.
17+
"Find Kth Largest" is equal to "Find [len(nums)-(k-1)]th Smallest"
18+
First, heapify the nums, taking O(N)
19+
Second, keep popping the smallest element for len(nums)-(k-1) times. Each taking O(LogN).
20+
"""
21+
import heapq
22+
23+
class Solution(object):
24+
def findKthLargest(self, nums, k):
25+
ans = 0
26+
k = len(nums)-(k-1)
27+
heapq.heapify(nums)
28+
29+
while k>0:
30+
ans = heapq.heappop(nums)
31+
k -= 1
32+
33+
return ans
34+
35+
"""
36+
Quick Search
37+
38+
Time: O(N) in average case, O(N^2) in worst case.
39+
Spcae: O(1)
40+
"""
41+
class Solution(object):
42+
def findKthLargest(self, nums, k):
43+
#to be continued...
44+
pass

problems/word-ladder.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,107 @@ def ladderLength(self, beginWord, endWord, wordList):
9595
q.append((next_word, steps+1))
9696
seen.add(next_word)
9797
return 0
98+
99+
100+
#2021/8/9
101+
"""
102+
Time: O(V^2), build the adjacent list takes V^2. BFS takes O(V+E).
103+
Space: O(V).
104+
105+
a = {
106+
'hit': ['hot'],
107+
'lot': ['hot', 'dot', 'lot', 'log'],
108+
'dog': ['dot', 'dog', 'log', 'cog'],
109+
'hot': ['hot', 'dot', 'lot'],
110+
'cog': ['dog', 'log', 'cog'],
111+
'dot': ['hot', 'dot', 'dog', 'lot'],
112+
'log': ['dog', 'lot', 'log', 'cog']
113+
}
114+
"""
115+
class Solution(object):
116+
def ladderLength(self, beginWord, endWord, wordList):
117+
#build an adjacent list
118+
a = collections.defaultdict(list)
119+
120+
for word in wordList+[beginWord]:
121+
possible = set()
122+
for i in xrange(len(word)):
123+
for alphabet in 'abcdefghijklmnopqrstuvwxyz':
124+
possible.add(word[:i]+alphabet+word[i+1:])
125+
for nextWord in wordList:
126+
if nextWord in possible:
127+
a[word].append(nextWord)
128+
129+
#BFS
130+
q = collections.deque([(beginWord, 1)])
131+
seen = set()
132+
133+
while q:
134+
word, steps = q.popleft()
135+
if word in seen: continue
136+
if word==endWord: return steps
137+
seen.add(word)
138+
for wordNext in a[word]: q.append((wordNext, steps+1))
139+
140+
return 0
141+
142+
143+
"""
144+
Time: O(VL+E), build the adjacent list takes O(VL). BFS takes O(V+E).
145+
Space: O(V).
146+
147+
Make some adjust ment to the adjacent list:
148+
a = {
149+
'lo#': ['lot', 'log'],
150+
'l#t': ['lot'],
151+
'#ot': ['hot', 'dot', 'lot'],
152+
'h#t': ['hot'],
153+
'do#': ['dot', 'dog'],
154+
'l#g': ['log'],
155+
'co#': ['cog'],
156+
'#og': ['dog', 'log', 'cog'],
157+
'd#g': ['dog'],
158+
'd#t': ['dot'],
159+
'c#g': ['cog'],
160+
'ho#': ['hot']
161+
}
162+
"""
163+
class Solution(object):
164+
def ladderLength(self, beginWord, endWord, wordList):
165+
#build an adjacent list
166+
a = collections.defaultdict(list)
167+
168+
for word in wordList+[beginWord]:
169+
for i in xrange(len(word)):
170+
s = word[:i]+'#'+word[i+1:]
171+
a[s].append(word)
172+
173+
#BFS
174+
q = collections.deque([(beginWord, 1)])
175+
seen = set()
176+
177+
while q:
178+
word, steps = q.popleft()
179+
if word in seen: continue
180+
if word==endWord: return steps
181+
seen.add(word)
182+
183+
for i in xrange(len(word)):
184+
s = word[:i]+'#'+word[i+1:]
185+
for wordNext in a[s]:
186+
q.append((wordNext, steps+1))
187+
188+
return 0
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+

0 commit comments

Comments
 (0)