|
| 1 | +# LeetCode 讀書會第 41 次聚會 2022/04/19 |
| 2 | + |
| 3 | +## leetcode 讀書會通知 |
| 4 | + |
| 5 | +1. 項目: 第 41 次聚會 |
| 6 | +2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法 |
| 7 | +3. 時間: 04/19 (二) 20:00 ~ 21:00 |
| 8 | +4. 地點: google meet 線上 (前 10 分鐘預備鏈接) |
| 9 | +5. 解題項目: [Heap](https://leetcode.com/explore/featured/card/heap/643/heap/) |
| 10 | +6. 共筆: GitHub https://github.com/programmingbookclub/Leetcode-club |
| 11 | +7. 備註: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +* [done] MEDIUM 215 Kth Largest Element in an Array https://leetcode.com/problems/kth-largest-element-in-an-array |
| 18 | +* [done] MEDIUM 347 Top K Frequent Elements https://leetcode.com/problems/top-k-frequent-elements |
| 19 | +* EASY 703 Kth Largest Element in a Stream https://leetcode.com/problems/kth-largest-element-in-a-stream |
| 20 | +* EASY 1046 Last Stone Weight https://leetcode.com/problems/last-stone-weight |
| 21 | +* EASY 1337 The K Weakest Rows in a Matrix https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix |
| 22 | +* MEDIUM 378 Kth Smallest Element in a Sorted Matrix https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix |
| 23 | +* 🔓 MEDIUM 253 Meeting Rooms II https://leetcode.com/problems/meeting-rooms-ii |
| 24 | +* MEDIUM 973 K Closest Points to Origin https://leetcode.com/problems/k-closest-points-to-origin |
| 25 | +* 🔓 MEDIUM 1167 Minimum Cost to Connect Sticks https://leetcode.com/problems/minimum-cost-to-connect-sticks |
| 26 | +* MEDIUM 1642 Furthest Building You Can Reach https://leetcode.com/problems/furthest-building-you-can-reach |
| 27 | +* HARD 295 Find Median from Data Stream https://leetcode.com/problems/find-median-from-data-stream |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +``` python |
| 32 | +import heapq |
| 33 | +class KthLargest: |
| 34 | + # clarification: |
| 35 | + # only add? |
| 36 | + # upperbound/lowerbound of stream element |
| 37 | + # |
| 38 | + # idea: minheap with fixed size |
| 39 | + # push the new element into heap, update the heap (if the size is too big), return heap[0] |
| 40 | + # tc: |
| 41 | + # O(nlogn) initialize, best case O(n) |
| 42 | + # O(logn) add, best case O(1) |
| 43 | + # sc: |
| 44 | + # O(k) to store elements of stream |
| 45 | + def __init__(self, k: int, nums: List[int]): |
| 46 | + self.size = k |
| 47 | + self.hp = nums |
| 48 | + heapq.heapify(self.hp) |
| 49 | + while len(self.hp) > k: |
| 50 | + heapq.heappop(self.hp) |
| 51 | + |
| 52 | + def add(self, val: int) -> int: |
| 53 | + heapq.heappush(self.hp, val) |
| 54 | + if len(self.hp) > self.size: |
| 55 | + heapq.heappop(self.hp) |
| 56 | + return self.hp[0] |
| 57 | +``` |
| 58 | + |
| 59 | +``` python |
| 60 | +import heapq |
| 61 | +class Solution: |
| 62 | + def lastStoneWeight(self, stones: List[int]) -> int: |
| 63 | + # idea: |
| 64 | + # use max-heap, (use negative element while inserting into built-in minheap) |
| 65 | + # put the difference back (even diff == 0) until heap size == 1 |
| 66 | + hp = [-s for s in stones] |
| 67 | + heapq.heapify(hp) |
| 68 | + |
| 69 | + while len(hp) != 1: |
| 70 | + a = heapq.heappop(hp) |
| 71 | + b = heapq.heappop(hp) |
| 72 | + heapq.heappush(hp, -1*abs(a-b)) |
| 73 | + |
| 74 | + return -1*hp[0] |
| 75 | +``` |
| 76 | + |
| 77 | +``` python |
| 78 | +import heapq |
| 79 | +class Solution: |
| 80 | + # use maxHeap |
| 81 | + # calculate the sum of each row and store the (row_sum, index) into max_heap |
| 82 | + # if the size is too big, pop out the biggest in the heap |
| 83 | + # if the two biggest has the same sum, pop out the one has bigger index |
| 84 | + # |
| 85 | + # tc: O(H*W) to go through the matrix, where H is height and W is width. O(klogk) to pick out k weakest row index |
| 86 | + # sc: O(k) to store k weakest rows |
| 87 | + def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: |
| 88 | + hp = [] # store (row_sum, index) in the heap |
| 89 | + for i, row in enumerate(mat): |
| 90 | + x = sum(row) |
| 91 | + heapq.heappush(hp, (-1*x, -1*i)) # both use negative due to built-in minHeap |
| 92 | + if len(hp) > k: |
| 93 | + heapq.heappop(hp) |
| 94 | + |
| 95 | + res = [] # output the answer in order |
| 96 | + while hp: |
| 97 | + ppl, index = heapq.heappop(hp) |
| 98 | + res.append(-1*index) |
| 99 | + |
| 100 | + return res[::-1] |
| 101 | +``` |
| 102 | + |
| 103 | +() |
0 commit comments