Skip to content

Commit 0e4a7cc

Browse files
authored
Merge pull request #13 from tiationg-kho/update
update
2 parents ae7b80a + 087e600 commit 0e4a7cc

10 files changed

+189
-44
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ Pattern500 is inspired by Blind 75 and Grind 75's classic problem selection. Pat
174174
| 160 | O | [295. Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [E]heap | two heap problem | [Python]([E]heap/[E]heap/295-find-median-from-data-stream.py) |
175175
| 161 | | [502. IPO](https://leetcode.com/problems/ipo/) | [E]heap | two heap problem | [Python]([E]heap/[E]heap/502-ipo.py) |
176176
| 162 | | [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [E]heap | two heap problem | [Python]([E]heap/[E]heap/480-sliding-window-median.py) |
177-
| 163 | | [1383. Maximum Performance of a Team](https://leetcode.com/problems/maximum-performance-of-a-team/) | [E]heap | focus on stored elements | [Python]([E]heap/[E]heap/1383-maximum-performance-of-a-team.py) |
178-
| 164 | O | [XX]() | [X]XX | XX | [Python]() |
179-
| 165 | O | [XX]() | [X]XX | XX | [Python]() |
180-
| 166 | | [1705. Maximum Number of Eaten Apples](https://leetcode.com/problems/maximum-number-of-eaten-apples/) | [E]heap | focus on popping out | [Python]([E]heap/[E]heap/1705-maximum-number-of-eaten-apples.py) |
181-
| 167 | O | [XX]() | [X]XX | XX | [Python]() |
182-
| 168 | O | [XX]() | [X]XX | XX | [Python]() |
183-
| 169 | O | [XX]() | [X]XX | XX | [Python]() |
184-
| 170 | O | [XX]() | [X]XX | XX | [Python]() |
177+
| 163 | | [1383. Maximum Performance of a Team](https://leetcode.com/problems/maximum-performance-of-a-team/) | [E]heap | storing and popping out elements | [Python]([E]heap/[E]heap/1383-maximum-performance-of-a-team.py) |
178+
| 164 | | [630. Course Schedule III](https://leetcode.com/problems/course-schedule-iii/) | [E]heap | storing and popping out elements | [Python]([E]heap/[E]heap/630-course-schedule-iii.py) |
179+
| 165 | | [1094. Car Pooling](https://leetcode.com/problems/car-pooling/) | [E]heap | storing and popping out elements | [Python]([E]heap/[E]heap/1094-car-pooling.py) |
180+
| 166 | | [1705. Maximum Number of Eaten Apples](https://leetcode.com/problems/maximum-number-of-eaten-apples/) | [E]heap | storing and popping out elements | [Python]([E]heap/[E]heap/1705-maximum-number-of-eaten-apples.py) |
181+
| 167 | | [1792. Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) | [E]heap | storing and popping out elements | [Python]([E]heap/[E]heap/1792-maximum-average-pass-ratio.py) |
182+
| 168 | | [378. Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [E]heap | use bfs and heap | [Python]([E]heap/[E]heap/378-kth-smallest-element-in-a-sorted-matrix.py) |
183+
| 169 | | [373. Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | [E]heap | use bfs and heap | [Python]([E]heap/[E]heap/373-find-k-pairs-with-smallest-sums.py) |
184+
| 170 | | [407. Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) | [E]heap | use bfs and heap | [Python]([E]heap/[E]heap/407-trapping-rain-water-ii.py) |
185185
| 171 | | [string]([F]string/string.md) | [F]string | | |
186186
| 172 | O | [XX]() | [X]XX | XX | [Python]() |
187187
| 173 | O | [XX]() | [X]XX | XX | [Python]() |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from heapq import *
2+
class Solution:
3+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
4+
trips.sort(key = lambda x: x[1])
5+
6+
seats = capacity
7+
heap = []
8+
for p, s, e in trips:
9+
while heap and heap[0][0] <= s:
10+
prev_e, prev_p = heappop(heap)
11+
seats += prev_p
12+
if seats < p:
13+
return False
14+
heappush(heap, (e, p))
15+
seats -= p
16+
return True
17+
18+
# time O(nlogn), due to sort, and traverse and perform heap operations
19+
# space O(n), due to heap
20+
# using heap and storing and popping out elements and sort
21+
'''
22+
1. sort by start time
23+
2. store (end, passengers) in heap
24+
3. when new passengers come, let old passengers leave (with smallest end time)
25+
'''

[E]heap/[E]heap/1383-maximum-performance-of-a-team.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
from heapq import *
22
class Solution:
33
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
4-
eng = [(e, s) for e, s in zip(efficiency, speed)]
5-
eng.sort(key = lambda x: - x[0])
4+
5+
engineers = [(s, e) for s, e in zip(speed, efficiency)]
6+
engineers.sort(key = lambda x: - x[1])
67

78
res = 0
9+
total = 0
810
heap = []
9-
total_speed = 0
10-
for e, s in eng:
11-
total_speed += s
11+
for s, e in engineers:
12+
total += s
1213
heappush(heap, s)
1314
if len(heap) > k:
14-
total_speed -= heappop(heap)
15-
res = max(res, e * total_speed)
16-
return res % (10**9 + 7)
15+
total -= heappop(heap)
16+
res = max(res, total * e)
17+
18+
return res % (10**9+7)
1719

1820
# time O(nlogn)
1921
# space O(n + k)
20-
# using heap and focus on stored elements and sort and greedy
22+
# using heap and storing and popping out elements and sort and greedy
2123
'''
2224
two factors in problem, need to handle ony by one
2325
1. we traverse every possible team effi (from high to low), and keep recording the best perf
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
from heapq import *
22
class Solution:
33
def eatenApples(self, apples: List[int], days: List[int]) -> int:
4-
res = 0
54
heap = []
5+
res = 0
66
for i in range(len(apples)):
7-
count = apples[i]
8-
expire = i + days[i]
9-
if count:
10-
heappush(heap, [expire, count])
11-
while heap and (heap[0][0] <= i or heap[0][1] == 0):
7+
while heap and heap[0][0] <= i:
128
heappop(heap)
9+
if apples[i] != 0:
10+
heappush(heap, [i + days[i], apples[i]])
1311
if heap and heap[0][1] > 0:
1412
res += 1
1513
heap[0][1] -= 1
14+
if heap[0][1] == 0:
15+
heappop(heap)
1616

17-
cur_day = len(apples)
17+
cur = len(apples)
1818
while heap:
19-
expire, count = heappop(heap)
20-
if expire <= cur_day:
21-
continue
22-
eat = min(count, expire - cur_day)
23-
res += eat
24-
cur_day += eat
19+
while heap and heap[0][0] <= cur:
20+
heappop(heap)
21+
if heap:
22+
end, count = heappop(heap)
23+
res += min(end - cur, count)
24+
cur += min(end - cur, count)
25+
2526
return res
2627

2728
# time O(nlogn)
2829
# space O(n)
29-
# using heap and focus on popping out
30+
# using heap and storing and popping out elements
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from heapq import *
2+
class Solution:
3+
def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
4+
heap = [(-((p+1)/(t+1) - p/t), p, t) for p, t in classes]
5+
heapify(heap)
6+
7+
while extraStudents:
8+
_, p, t = heappop(heap)
9+
heappush(heap, (-((p+2)/(t+2) - (p+1)/(t+1)), p + 1, t + 1))
10+
extraStudents -= 1
11+
12+
ratio = 0
13+
for _, p, t in heap:
14+
ratio += p/t
15+
return ratio / len(classes)
16+
17+
# time O(n + mlogn)
18+
# space O(n)
19+
# using heap and storing and popping out elements
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from heapq import *
2+
class Solution:
3+
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
4+
heap = []
5+
visited = set()
6+
heappush(heap, (nums1[0] + nums2[0], 0, 0))
7+
visited.add((0, 0))
8+
res = []
9+
while len(res) < k:
10+
val, i, j = heappop(heap)
11+
res.append([nums1[i], nums2[j]])
12+
if i + 1 < len(nums1) and (i + 1, j) not in visited:
13+
heappush(heap, (nums1[i + 1] + nums2[j], i + 1, j))
14+
visited.add((i + 1, j))
15+
if j + 1 < len(nums2) and (i, j + 1) not in visited:
16+
heappush(heap, (nums1[i] + nums2[j + 1], i, j + 1))
17+
visited.add((i, j + 1))
18+
return res
19+
20+
# time O(klogk)
21+
# space O(k)
22+
# using heap and bfs
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from heapq import *
2+
class Solution:
3+
def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
4+
rows, cols = len(matrix), len(matrix[0])
5+
heap = []
6+
visited = set()
7+
heappush(heap, (matrix[0][0], 0, 0))
8+
visited.add((0, 0))
9+
while k:
10+
val, r, c = heappop(heap)
11+
k -= 1
12+
if r + 1 < rows and (r + 1, c) not in visited:
13+
heappush(heap, (matrix[r+1][c], r + 1, c))
14+
visited.add((r + 1, c))
15+
if c + 1 < cols and (r, c + 1) not in visited:
16+
heappush(heap, (matrix[r][c+1], r, c + 1))
17+
visited.add((r, c + 1))
18+
return val
19+
20+
# time O(klogk)
21+
# space O(k)
22+
# using heap and bfs
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from heapq import *
2+
class Solution:
3+
def trapRainWater(self, heightMap: List[List[int]]) -> int:
4+
h = heightMap
5+
rows, cols = len(h), len(h[0])
6+
heap = []
7+
visited = set()
8+
for r in range(rows):
9+
for c in range(cols):
10+
if (r == 0 or r == rows - 1) and (r, c) not in visited:
11+
heappush(heap, (h[r][c], r, c))
12+
visited.add((r, c))
13+
if (c == 0 or c == cols - 1) and (r, c) not in visited:
14+
heappush(heap, (h[r][c], r, c))
15+
visited.add((r, c))
16+
17+
res = 0
18+
while heap:
19+
height, r, c = heappop(heap)
20+
for next_r, next_c in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]:
21+
if next_r not in range(rows) or next_c not in range(cols):
22+
continue
23+
if (next_r, next_c) in visited:
24+
continue
25+
res += max(height - h[next_r][next_c], 0)
26+
heappush(heap, (max(height, h[next_r][next_c]), next_r, next_c))
27+
visited.add((next_r, next_c))
28+
return res
29+
30+
# time O(RClog(RC)), heap size is RC
31+
# space O(RC), due to hashset
32+
# using heap and bfs
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from heapq import *
2+
class Solution:
3+
def scheduleCourse(self, courses: List[List[int]]) -> int:
4+
courses.sort(key = lambda x: x[1])
5+
6+
heap = []
7+
cur_day = 0
8+
for duration, lastday in courses:
9+
if cur_day + duration <= lastday:
10+
heappush(heap, - duration)
11+
cur_day += duration
12+
elif heap and - heap[0] > duration:
13+
cur_day -= - (heappop(heap))
14+
heappush(heap, - duration)
15+
cur_day += duration
16+
17+
return len(heap)
18+
19+
# time O(nlogn)
20+
# space O(n)
21+
# using heap and storing and popping out elements and sort and greedy
22+
'''
23+
1. greedy strategy
24+
2. we consider the course with earilier deadline first
25+
3. if we can take, we push in heap
26+
4. if we can not take, we should check any prev course can be replaced or not
27+
5. if so, we pop out that course with longest time, then take cur course (cur course is better choice)
28+
6. if not, we discard cur course
29+
'''

[E]heap/heap.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,13 @@
9191
- use hashmap to record the freq of removed elements (not really removed yet)
9292
- maintain heap size variable to keep track of the real heap size (exclude the removed elements which recorded in hashmap)
9393

94-
**focus on stored elements**
94+
**storing and popping out elements**
9595

96-
- use heap to keep elements (min or max group)
97-
- keep store and pop out the invalid elements
98-
99-
**focus on popping out**
100-
101-
- use heap to get the cur min or max element
102-
- contains greedy algorithm’s idea
103-
- we use popping out to get the most reasonable element to utilize
96+
- use heap to store elements (min or max group)
97+
- keep popping out the invalid or useful element (comparing to cur condition)
98+
- contains greedy algorithm’s idea
10499

105100
**use bfs and heap**
106101

107-
- this is one variation of focusing on popping out
108-
- use bfs to find potential valid elements
109-
- use hashset to record visited elements
110-
- use heap to get the cur min or max element
102+
- use bfs to find potential valid elements (use heap to replace the queue)
103+
- use hashset to record visited elements

0 commit comments

Comments
 (0)