Skip to content

Commit a3de0ee

Browse files
committed
update
1 parent 2dbcd1c commit a3de0ee

15 files changed

+641
-15
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,18 @@ Pattern500 is inspired by Blind 75 and Grind 75's classic problem selection. Pat
296296
| 282 | | [344. Reverse String](https://leetcode.com/problems/reverse-string/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/344-reverse-string.py) |
297297
| 283 | | [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/151-reverse-words-in-a-string.py) |
298298
| 284 | O | [5. Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [H]array-two-pointers-opposite-direction | use expand type | [Python]([H]array/[H]array-two-pointers-opposite-direction/5-longest-palindromic-substring.py) |
299-
| 285 | O | [XX]() | [X]XX | XX | [Python]() |
300-
| 286 | O | [XX]() | [X]XX | XX | [Python]() |
301-
| 287 | O | [XX]() | [X]XX | XX | [Python]() |
302-
| 288 | O | [XX]() | [X]XX | XX | [Python]() |
303-
| 289 | O | [XX]() | [X]XX | XX | [Python]() |
304-
| 290 | O | [XX]() | [X]XX | XX | [Python]() |
305-
| 291 | O | [XX]() | [X]XX | XX | [Python]() |
306-
| 292 | O | [XX]() | [X]XX | XX | [Python]() |
307-
| 293 | O | [XX]() | [X]XX | XX | [Python]() |
308-
| 294 | O | [XX]() | [X]XX | XX | [Python]() |
309-
| 295 | O | [XX]() | [X]XX | XX | [Python]() |
310-
| 296 | O | [XX]() | [X]XX | XX | [Python]() |
299+
| 285 | O | [179. Largest Number](https://leetcode.com/problems/largest-number/) | [H]array-sort | use self defined sort | [Python]([H]array/[H]array-sort/179-largest-number.py) |
300+
| 286 | O | [215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [H]array-sort | top k problem (based on sort) | [Python]([H]array/[H]array-sort/215-kth-largest-element-in-an-array.py) |
301+
| 287 | | [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [H]array-sort | top k problem (based on sort) | [Python]([H]array/[H]array-sort/347-top-k-frequent-elements.py) |
302+
| 288 | | [1985. Find the Kth Largest Integer in the Array](https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/) | [H]array-sort | top k problem (based on sort) | [Python]([H]array/[H]array-sort/1985-find-the-kth-largest-integer-in-the-array.py) |
303+
| 289 | | [1710. Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | [H]array-sort | use bucket sort | [Python]([H]array/[H]array-sort/1710-maximum-units-on-a-truck.py) |
304+
| 290 | | [451. Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | [H]array-sort | use bucket sort | [Python]([H]array/[H]array-sort/451-sort-characters-by-frequency.py) |
305+
| 291 | | [1099. Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [H]array-sort | use bucket sort | [Python]([H]array/[H]array-sort/1099-two-sum-less-than-k.py) |
306+
| 292 | | [164. Maximum Gap](https://leetcode.com/problems/maximum-gap/) | [H]array-sort | use bucket sort | [Python]([H]array/[H]array-sort/164-maximum-gap.py) |
307+
| 293 | | [462. Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [H]array-sort | use quick select | [Python]([H]array/[H]array-sort/462-minimum-moves-to-equal-array-elements-ii.py) |
308+
| 294 | | [324. Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [H]array-sort | use quick select | [Python]([H]array/[H]array-sort/324-wiggle-sort-ii.py) |
309+
| 295 | | [315. Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [H]array-sort | use merge sort | [Python]([H]array/[H]array-sort/315-count-of-smaller-numbers-after-self.py) |
310+
| 296 | | [493. Reverse Pairs](https://leetcode.com/problems/reverse-pairs/) | [H]array-sort | use merge sort | [Python]([H]array/[H]array-sort/493-reverse-pairs.py) |
311311
| 297 | | [stack-queue]([I]stack-queue/stack-queue.md) | [I]stack-queue | | |
312312
| 298 | | [346. Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) | [I]stack-queue | use queue to simulate | [Python]([I]stack-queue/[I]stack-queue/346-moving-average-from-data-stream.py) |
313313
| 299 | O | [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/20-valid-parentheses.py) |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def twoSumLessThanK(self, nums: List[int], k: int) -> int:
3+
min_num = min(nums)
4+
max_num = max(nums)
5+
buckets = [0 for _ in range(max_num - min_num + 1)]
6+
for num in nums:
7+
buckets[num - min_num] += 1
8+
9+
res = - 1
10+
left, right = 0, len(buckets) - 1
11+
while left <= right:
12+
cur = left + min_num + right + min_num
13+
if buckets[left] == 0:
14+
left += 1
15+
elif buckets[right] == 0:
16+
right -= 1
17+
elif left == right and buckets[left] < 2:
18+
break
19+
elif cur < k:
20+
res = max(res, cur)
21+
left += 1
22+
else:
23+
right -= 1
24+
return res
25+
26+
# time O(n + b)
27+
# space O(b)
28+
# using array and sort and bucket sort and two pointers
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def maximumGap(self, nums: List[int]) -> int:
3+
min_num = min(nums)
4+
max_num = max(nums)
5+
if min_num == max_num or len(nums) < 2:
6+
return 0
7+
8+
interval = max(1, (max_num - min_num) // len(nums))
9+
buckets_count = (max_num - min_num) // interval + 1
10+
buckets = [[False, float('inf'), float('-inf')] for _ in range(buckets_count)]
11+
for num in nums:
12+
idx = (num - min_num) // interval
13+
buckets[idx] = [True, min(buckets[idx][1], num), max(buckets[idx][2], num)]
14+
15+
buckets = [[min_val, max_val] for filled, min_val, max_val in buckets if filled]
16+
res = 0
17+
for i in range(1, len(buckets)):
18+
res = max(res, buckets[i][0] - buckets[i - 1][1])
19+
20+
return res
21+
22+
# time O(n + b)
23+
# space O(b)
24+
# using array and sort and bucket sort and pigeonhole principle
25+
'''
26+
1. pigeonhole principle
27+
2. n+1 pigeons live in n holes, at least one hole have two or more pigeons
28+
3. n pigeons live in n+1 holes, at least one hole is empty
29+
4. so we need to make buckets more than length of nums
30+
5. if there is a empty hole,
31+
the maximum difference cannot be within a single bucket but between the numbers in different buckets
32+
6. even if all buckets are filled,
33+
the maximum gap is still likely to occur between the maximum value of one bucket and the minimum value of an adjacent bucket
34+
this is because the numbers within each bucket are relatively close to each other, given the way the interval size is determined
35+
'''
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
3+
4+
max_box_size = max(boxTypes, key = lambda x: x[1])[1]
5+
min_box_size = min(boxTypes, key = lambda x: x[1])[1]
6+
7+
buckets = [0 for _ in range(max_box_size - min_box_size + 1)]
8+
for count, size in boxTypes:
9+
buckets[size - min_box_size] += count
10+
11+
res = 0
12+
for i in range(len(buckets) - 1, - 1, - 1):
13+
if buckets[i] <= truckSize:
14+
res += buckets[i] * (i + min_box_size)
15+
truckSize -= buckets[i]
16+
elif truckSize:
17+
res += truckSize * (i + min_box_size)
18+
truckSize -= truckSize
19+
else:
20+
break
21+
return res
22+
23+
# time O(n+b)
24+
# space O(b)
25+
# using array and sort and bucket sort and greedy
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Solution:
2+
def largestNumber(self, nums: List[int]) -> str:
3+
4+
nums = [str(num) for num in nums]
5+
6+
def merge_sort(nums):
7+
n = len(nums)
8+
if n <= 1:
9+
return nums
10+
m = n // 2
11+
left_part = merge_sort(nums[: m])
12+
right_part = merge_sort(nums[m:])
13+
res = []
14+
left, right = 0, 0
15+
while left < m or right < n - m:
16+
if left == m:
17+
res.append(right_part[right])
18+
right += 1
19+
elif right == n- m:
20+
res.append(left_part[left])
21+
left += 1
22+
elif left_part[left] + right_part[right] >= right_part[right] + left_part[left]:
23+
res.append(left_part[left])
24+
left += 1
25+
else:
26+
res.append(right_part[right])
27+
right += 1
28+
return res
29+
30+
nums = merge_sort(nums)
31+
res = ''.join(nums)
32+
return res if res[0] != '0' else '0'
33+
34+
# time O(nlogn), logn recursion layers and each layer costs O(n)
35+
# space O(n), due to new list, recursion stack is O(logn)
36+
# using array and sort and self defined merge sort
37+
# stable
38+
39+
import random
40+
class Solution:
41+
def largestNumber(self, nums: List[int]) -> str:
42+
43+
nums = [str(num) for num in nums]
44+
45+
def quick_sort(left, right):
46+
if left >= right:
47+
return
48+
pivot_idx = random.randint(left, right)
49+
pivot_val = nums[pivot_idx]
50+
nums[right], nums[pivot_idx] = nums[pivot_idx], nums[right]
51+
partition_idx = left
52+
for i in range(left, right):
53+
if nums[i] + pivot_val > pivot_val + nums[i]:
54+
nums[i], nums[partition_idx] = nums[partition_idx], nums[i]
55+
partition_idx += 1
56+
nums[right], nums[partition_idx] = nums[partition_idx], nums[right]
57+
quick_sort(left, partition_idx - 1)
58+
quick_sort(partition_idx + 1, right)
59+
return
60+
61+
quick_sort(0, len(nums) - 1)
62+
res = ''.join(nums)
63+
return res if res[0] != '0' else '0'
64+
65+
# time O(n**2), when the list is almost sorted, and the average time is O(nlogn)
66+
# space O(n), due to recursion layers, O(logn) in average
67+
# using array and sort and self defined quick sort
68+
# unstable
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections import defaultdict
2+
import random
3+
class Solution:
4+
def kthLargestNumber(self, nums: List[str], k: int) -> str:
5+
6+
nums = [int(num) for num in nums]
7+
8+
num_freq = defaultdict(int)
9+
for num in nums:
10+
num_freq[num] += 1
11+
num_with_max_freq = max(num_freq, key=num_freq.get)
12+
max_freq = max(num_freq.values())
13+
if max_freq > k and max_freq > len(nums) - k:
14+
return str(num_with_max_freq)
15+
16+
def quick_select(left, right):
17+
pivot_idx = random.randint(left, right)
18+
pivot_val = nums[pivot_idx]
19+
nums[right], nums[pivot_idx] = nums[pivot_idx], nums[right]
20+
partition_idx = left
21+
for i in range(left, right):
22+
if nums[i] < pivot_val:
23+
nums[i], nums[partition_idx] = nums[partition_idx], nums[i]
24+
partition_idx += 1
25+
nums[right], nums[partition_idx] = nums[partition_idx], nums[right]
26+
return partition_idx
27+
28+
left, right = 0, len(nums) - 1
29+
while left <= right:
30+
idx = quick_select(left, right)
31+
if idx == len(nums) - k:
32+
return str(nums[idx])
33+
elif idx > len(nums) - k:
34+
right = idx - 1
35+
else:
36+
left = idx + 1
37+
38+
# time O(n**2), quick select can be O(n) in average (O(n**2) in worst) (notice that quick sort is O(nlogn) in average)
39+
# space O(n)
40+
# using array and sort and top k problem (based on sort) and quick select and prune

0 commit comments

Comments
 (0)