Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,24 @@ Pattern500 is inspired by Blind 75 and Grind 75's classic problem selection. Pat
| 146 | O | [XX]() | [X]XX | XX | [Python]() |
| 147 | | [heap]([E]heap/heap.md) | [E]heap | | |
| 148 | O | [1235. Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/) | [E]heap | greedily schedule tasks (start/end/val) | [Python]([E]heap/[E]heap/1235-maximum-profit-in-job-scheduling.py) |
| 149 | O | [XX]() | [X]XX | XX | [Python]() |
| 149 | | [646. Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [E]heap | greedily schedule tasks (start/end/val) | [Python]([E]heap/[E]heap/646-maximum-length-of-pair-chain.py) |
| 150 | O | [XX]() | [X]XX | XX | [Python]() |
| 151 | O | [XX]() | [X]XX | XX | [Python]() |
| 152 | O | [XX]() | [X]XX | XX | [Python]() |
| 152 | O | [973. K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [E]heap | top k problem (based on heap) | [Python]([E]heap/[E]heap/973-k-closest-points-to-origin.py) |
| 153 | O | [XX]() | [X]XX | XX | [Python]() |
| 154 | O | [XX]() | [X]XX | XX | [Python]() |
| 154 | | [264. Ugly Number II](https://leetcode.com/problems/ugly-number-ii/) | [E]heap | k way merge problem | [Python]([E]heap/[E]heap/264-ugly-number-ii.py) |
| 155 | O | [XX]() | [X]XX | XX | [Python]() |
| 156 | O | [XX]() | [X]XX | XX | [Python]() |
| 157 | O | [XX]() | [X]XX | XX | [Python]() |
| 158 | O | [XX]() | [X]XX | XX | [Python]() |
| 159 | O | [XX]() | [X]XX | XX | [Python]() |
| 160 | O | [XX]() | [X]XX | XX | [Python]() |
| 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) |
| 161 | O | [XX]() | [X]XX | XX | [Python]() |
| 162 | O | [XX]() | [X]XX | XX | [Python]() |
| 163 | O | [XX]() | [X]XX | XX | [Python]() |
| 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) |
| 164 | O | [XX]() | [X]XX | XX | [Python]() |
| 165 | O | [XX]() | [X]XX | XX | [Python]() |
| 166 | O | [XX]() | [X]XX | XX | [Python]() |
| 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) |
| 167 | O | [XX]() | [X]XX | XX | [Python]() |
| 168 | O | [XX]() | [X]XX | XX | [Python]() |
| 169 | O | [XX]() | [X]XX | XX | [Python]() |
Expand Down
26 changes: 26 additions & 0 deletions [E]heap/[E]heap/1383-maximum-performance-of-a-team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from heapq import *
class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
eng = [(e, s) for e, s in zip(efficiency, speed)]
eng.sort(key = lambda x: - x[0])

res = 0
heap = []
total_speed = 0
for e, s in eng:
total_speed += s
heappush(heap, s)
if len(heap) > k:
total_speed -= heappop(heap)
res = max(res, e * total_speed)
return res % (10**9 + 7)

# time O(nlogn)
# space O(n + k)
# using heap and focus on stored elements and sort and greedy
'''
two factors in problem, need to handle ony by one
1. we traverse every possible team effi (from high to low), and keep recording the best perf
2. we handle the effi factor by sort, so we can guarantee the effi we use is monotonic dec
3. we handle the spee factor by heap, so we can only store the higher spee and pop the smallest
'''
29 changes: 29 additions & 0 deletions [E]heap/[E]heap/1705-maximum-number-of-eaten-apples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from heapq import *
class Solution:
def eatenApples(self, apples: List[int], days: List[int]) -> int:
res = 0
heap = []
for i in range(len(apples)):
count = apples[i]
expire = i + days[i]
if count:
heappush(heap, [expire, count])
while heap and (heap[0][0] <= i or heap[0][1] == 0):
heappop(heap)
if heap and heap[0][1] > 0:
res += 1
heap[0][1] -= 1

cur_day = len(apples)
while heap:
expire, count = heappop(heap)
if expire <= cur_day:
continue
eat = min(count, expire - cur_day)
res += eat
cur_day += eat
return res

# time O(nlogn)
# space O(n)
# using heap and focus on popping out
19 changes: 19 additions & 0 deletions [E]heap/[E]heap/264-ugly-number-ii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from heapq import *
class Solution:
def nthUglyNumber(self, n: int) -> int:
prime = [2, 3, 5]
res = [1]
heap = [(res[0] * prime[i], 0, i) for i in range(len(prime))]
while len(res) < n:
num, root_idx, prime_idx = heappop(heap)
if num > res[- 1]:
res.append(num)
heappush(heap, (res[root_idx + 1] * prime[prime_idx], root_idx + 1, prime_idx))
return res[- 1]

# time O((nk)*logk), k is 3
# space O(n + k)
# using heap and k way merge problem
'''
1. imagine 3 sorted list to do merging
'''
33 changes: 33 additions & 0 deletions [E]heap/[E]heap/295-find-median-from-data-stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from heapq import *
class MedianFinder:

def __init__(self):
self.maxheap_smallhalf = []
self.minheap_largehalf = []

def addNum(self, num: int) -> None:
heappush(self.maxheap_smallhalf, - num)
heappush(self.minheap_largehalf, heappop(self.maxheap_smallhalf) * (- 1))

if len(self.minheap_largehalf) - len(self.maxheap_smallhalf) > 1:
heappush(self.maxheap_smallhalf, heappop(self.minheap_largehalf) * (- 1))

def findMedian(self) -> float:
if (len(self.minheap_largehalf) + len(self.maxheap_smallhalf)) % 2 == 0:
return (self.maxheap_smallhalf[0] * (- 1) + self.minheap_largehalf[0]) / 2
return self.minheap_largehalf[0]

# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()

# time O(logn) for addNum(), O(1) for findMedian()
# space O(n), due to heap
# using heap and two heap problem
'''
follow up
bucket sort's idea
if input are in some small range, just build an array to record each number's count, then iterate over array to get median
also if most input are in some small range, will need build two counters to record number beyond range
'''
18 changes: 18 additions & 0 deletions [E]heap/[E]heap/646-maximum-length-of-pair-chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from heapq import *
class Solution:
def findLongestChain(self, pairs: List[List[int]]) -> int:
pairs.sort()
heap = []
prev_best = 0
all_time_best = 0
for s, e in pairs:
while heap and heap[0][0] < s:
_, prev_len = heappop(heap)
prev_best = max(prev_best, prev_len)
heappush(heap, (e, prev_best + 1))
all_time_best = max(all_time_best, prev_best + 1)
return all_time_best

# time O(nlogn), due to heap operation is O(logn) and sort is O(nlogn)
# space O(n), due to heap
# using heap and greedily schedule tasks (start/end/val) and sort and greedy
15 changes: 15 additions & 0 deletions [E]heap/[E]heap/973-k-closest-points-to-origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from heapq import *
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
heap = []
for x, y in points:
distance = (x**2 + y**2) ** 0.5
heappush(heap, (- distance, x, y))
if len(heap) > k:
heappop(heap)

return [[x, y] for _, x, y in heap]

# time O(nlogk)
# space O(k)
# using heap and top k problem (based on heap) and max heap
3 changes: 2 additions & 1 deletion [E]heap/heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
- use pop out elements to keep recording the previous best result
- the previous best result is according to cur task (also applicable for future tasks to use)
- push the cur end time and cur result in heap for future tasks
- when pushing also treat this profit as a candidate of best result
- when pushing also treat this cur result as a candidate of best result
- we need to keep recording the all time best result

**top k problem (based on heap)**
Expand All @@ -84,6 +84,7 @@
**two heap problem**

- normal two heap
- new element must get into proper heap
- lazy removal
- size
- hashmap
Expand Down