Skip to content

Commit 295946e

Browse files
authored
Merge pull request #17 from tiationg-kho/update
update
2 parents 60f4795 + 9e16380 commit 295946e

21 files changed

+599
-55
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,24 @@ Pattern500 is inspired by Blind 75 and Grind 75's classic problem selection. Pat
209209
| 195 | | [472. Concatenated Words](https://leetcode.com/problems/concatenated-words/) | [G]trie | perform dfs inside trie | [Python]([G]trie/[G]trie/472-concatenated-words.py) |
210210
| 196 | | [642. Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/) | [G]trie | perform dfs inside trie | [Python]([G]trie/[G]trie/642-design-search-autocomplete-system.py) |
211211
| 197 | | [array]([H]array/array.md) | [H]array | | |
212-
| 198 | O | [XX]() | [X]XX | XX | [Python]() |
213-
| 199 | O | [XX]() | [X]XX | XX | [Python]() |
214-
| 200 | O | [XX]() | [X]XX | XX | [Python]() |
215-
| 201 | O | [XX]() | [X]XX | XX | [Python]() |
216-
| 202 | O | [XX]() | [X]XX | XX | [Python]() |
217-
| 203 | O | [XX]() | [X]XX | XX | [Python]() |
218-
| 204 | O | [XX]() | [X]XX | XX | [Python]() |
219-
| 205 | O | [XX]() | [X]XX | XX | [Python]() |
220-
| 206 | O | [XX]() | [X]XX | XX | [Python]() |
221-
| 207 | O | [XX]() | [X]XX | XX | [Python]() |
222-
| 208 | O | [XX]() | [X]XX | XX | [Python]() |
223-
| 209 | O | [XX]() | [X]XX | XX | [Python]() |
224-
| 210 | O | [XX]() | [X]XX | XX | [Python]() |
225-
| 211 | O | [XX]() | [X]XX | XX | [Python]() |
226-
| 212 | O | [XX]() | [X]XX | XX | [Python]() |
227-
| 213 | O | [XX]() | [X]XX | XX | [Python]() |
228-
| 214 | O | [XX]() | [X]XX | XX | [Python]() |
229-
| 215 | O | [XX]() | [X]XX | XX | [Python]() |
212+
| 198 | | [243. Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [H]array | traverse | [Python]([H]array/[H]array/243-shortest-word-distance.py) |
213+
| 199 | O | [169. Majority Element](https://leetcode.com/problems/majority-element/) | [H]array | use boyer moore vote algorithm | [Python]([H]array/[H]array/169-majority-element.py) |
214+
| 200 | | [229. Majority Element II](https://leetcode.com/problems/majority-element-ii/) | [H]array | use boyer moore vote algorithm | [Python]([H]array/[H]array/229-majority-element-ii.py) |
215+
| 201 | O | [189. Rotate Array](https://leetcode.com/problems/rotate-array/) | [H]array | use reverse technique | [Python]([H]array/[H]array/189-rotate-array.py) |
216+
| 202 | O | [362. Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [H]array | use circular array | [Python]([H]array/[H]array/362-design-hit-counter.py) |
217+
| 203 | O | [41. First Missing Positive](https://leetcode.com/problems/first-missing-positive/) | [H]array | specific range array (cyclic sort) | [Python]([H]array/[H]array/41-first-missing-positive.py) |
218+
| 204 | | [442. Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [H]array | specific range array (cyclic sort) | [Python]([H]array/[H]array/442-find-all-duplicates-in-an-array.py) |
219+
| 205 | | [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [H]array | specific range array (cyclic sort) | [Python]([H]array/[H]array/448-find-all-numbers-disappeared-in-an-array.py) |
220+
| 206 | O | [287. Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [H]array | specific range array (cycle detection) | [Python]([H]array/[H]array/287-find-the-duplicate-number.py) |
221+
| 207 | | [845. Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [H]array | use finite state machine | [Python]([H]array/[H]array/845-longest-mountain-in-array.py) |
222+
| 208 | | [370. Range Addition](https://leetcode.com/problems/range-addition/) | [H]array | use difference array | [Python]([H]array/[H]array/370-range-addition.py) |
223+
| 209 | | [849. Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [H]array | count continuous elements | [Python]([H]array/[H]array/849-maximize-distance-to-closest-person.py) |
224+
| 210 | | [384. Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [H]array | use knuth shuffle | [Python]([H]array/[H]array/384-shuffle-an-array.py) |
225+
| 211 | | [398. Random Pick Index](https://leetcode.com/problems/random-pick-index/) | [H]array | use reservoir sampling | [Python]([H]array/[H]array/398-random-pick-index.py) |
226+
| 212 | | [1535. Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [H]array | simulation | [Python]([H]array/[H]array/1535-find-the-winner-of-an-array-game.py) |
227+
| 213 | | [280. Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [H]array | use swap | [Python]([H]array/[H]array/280-wiggle-sort.py) |
228+
| 214 | | [1472. Design Browser History](https://leetcode.com/problems/design-browser-history/) | [H]array | maintain array's range dynamically | [Python]([H]array/[H]array/1472-design-browser-history.py) |
229+
| 215 | | [163. Missing Ranges](https://leetcode.com/problems/missing-ranges/) | [H]array | pre-process the array | [Python]([H]array/[H]array/163-missing-ranges.py) |
230230
| 216 | O | [XX]() | [X]XX | XX | [Python]() |
231231
| 217 | O | [XX]() | [X]XX | XX | [Python]() |
232232
| 218 | O | [XX]() | [X]XX | XX | [Python]() |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class BrowserHistory:
2+
3+
def __init__(self, homepage: str):
4+
self.pages = [homepage]
5+
self.start = 0
6+
self.cur = 0
7+
self.end = 0
8+
9+
def visit(self, url: str) -> None:
10+
if self.cur + 1 == len(self.pages):
11+
self.pages.append(url)
12+
self.cur += 1
13+
self.end = self.cur
14+
else:
15+
self.pages[self.cur + 1] = url
16+
self.cur += 1
17+
self.end = self.cur
18+
19+
def back(self, steps: int) -> str:
20+
if self.cur - steps >= self.start:
21+
self.cur -= steps
22+
return self.pages[self.cur]
23+
else:
24+
self.cur = self.start
25+
return self.pages[self.cur]
26+
27+
def forward(self, steps: int) -> str:
28+
if self.cur + steps <= self.end:
29+
self.cur += steps
30+
return self.pages[self.cur]
31+
else:
32+
self.cur = self.end
33+
return self.pages[self.cur]
34+
35+
# Your BrowserHistory object will be instantiated and called as such:
36+
# obj = BrowserHistory(homepage)
37+
# obj.visit(url)
38+
# param_2 = obj.back(steps)
39+
# param_3 = obj.forward(steps)
40+
41+
# time O(1) for all
42+
# space O(n)
43+
# using array and maintain array's range dynamically
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def getWinner(self, arr: List[int], k: int) -> int:
3+
max_num = max(arr)
4+
5+
cur = None
6+
count = 0
7+
for i, num in enumerate(arr):
8+
if num == max_num:
9+
cur = num
10+
break
11+
if not cur:
12+
cur = num
13+
elif cur < num:
14+
cur = num
15+
count = 1
16+
else:
17+
count += 1
18+
if count == k:
19+
break
20+
return cur
21+
22+
# time O(n)
23+
# space O(1)
24+
# using array and simulation
25+
'''
26+
1. we can use queue to simulate the game
27+
2. but actually if we met max num, max num will always be res
28+
3. so, we do not need to consider element behind max num (involves the elements which were moved to end)
29+
'''
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:
3+
nums = [lower - 1] + nums + [upper + 1]
4+
res = []
5+
for i in range(1, len(nums)):
6+
prev = nums[i - 1]
7+
cur = nums[i]
8+
if cur - prev >= 2:
9+
res.append([prev + 1, cur - 1])
10+
return res
11+
12+
# time O(n)
13+
# space O(n)
14+
# using array and pre-process
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def majorityElement(self, nums: List[int]) -> int:
3+
cand = None
4+
vote = 0
5+
for num in nums:
6+
if num == cand:
7+
vote += 1
8+
elif cand == None:
9+
cand = num
10+
vote = 1
11+
else:
12+
vote -= 1
13+
if vote == 0:
14+
cand = None
15+
return cand
16+
17+
# time O(n)
18+
# space O(1)
19+
# using array and boyer moore vote algorithm
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def rotate(self, nums: List[int], k: int) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
7+
def helper(left, right):
8+
while left < right:
9+
nums[left], nums[right] = nums[right], nums[left]
10+
left += 1
11+
right -= 1
12+
13+
k %= len(nums)
14+
helper(0, len(nums) - 1)
15+
helper(0, k - 1)
16+
helper(k, len(nums) - 1)
17+
18+
# time O(n)
19+
# space O(1)
20+
# using array and reverse technique
21+
'''
22+
1. after rotate, can divide array into two part
23+
2. rotate them again
24+
'''

0 commit comments

Comments
 (0)