Skip to content

Commit 26394d7

Browse files
authored
Update 45 2022 0614 heap 8th.md
1 parent b914cf4 commit 26394d7

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

紀錄/45 2022 0614 heap 8th.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
8. 認真在考慮之後來看面試綠皮書
1313

1414

15-
---
1615

1716
* [done] MEDIUM 215 Kth Largest Element in an Array https://leetcode.com/problems/kth-largest-element-in-an-array
1817
* [done] MEDIUM 347 Top K Frequent Elements https://leetcode.com/problems/top-k-frequent-elements
@@ -24,8 +23,93 @@
2423
* [done] MEDIUM 973 K Closest Points to Origin https://leetcode.com/problems/k-closest-points-to-origin
2524
* 🔓 MEDIUM 1167 Minimum Cost to Connect Sticks https://leetcode.com/problems/minimum-cost-to-connect-sticks
2625
* [done] 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
26+
* [done] HARD 295 Find Median from Data Stream https://leetcode.com/problems/find-median-from-data-stream
2827
* MEDIUM 1631 Path With Minimum Effort https://leetcode.com/problems/path-with-minimum-effort/
2928

3029
---
3130

31+
```python
32+
"""
33+
295
34+
"""
35+
from heapq import * # In python the heap is a MIN heap
36+
37+
class MedianFinder:
38+
def __init__(self):
39+
self.small = [] # Stores the number that smaller than median, a max heap (* -1)
40+
self.large = [] # Stores the number that larger than median, a min heap
41+
self.size = 0 # Stores the size of data
42+
43+
def addNum(self, num):
44+
heappush(self.small, (-1)*heappushpop(self.large, num))
45+
self.size += 1
46+
47+
# Adjust the size of both heap,
48+
while len(self.large) < len(self.small): # keep the large always slightly bigger
49+
heappush(self.large, (-1)*heappop(self.small))
50+
51+
def findMedian(self):
52+
if self.size%2:
53+
return float(self.large[0])
54+
else:
55+
return float(self.large[0] + (-1)*self.small[0])/2.0
56+
```
57+
---
58+
59+
cat ... | do...
60+
61+
curl | ...
62+
stdinput
63+
64+
65+
Y.J. Lee下午8:02
66+
XD
67+
Y.J. Lee下午8:07
68+
Python就是之前ML很紅的時候很夯
69+
Y.J. Lee下午8:08
70+
有滿多免費的ML的demo專案
71+
幾乎一定會碰到visualization的東西
72+
不難啦 都是直接用library
73+
sry, 我好像每次都直接貼中間 qq
74+
Y.J. Lee下午8:17
75+
一樣的 ^^
76+
無腦調整 size
77+
Y.J. Lee下午8:19
78+
add的時候先上鎖 加完在解鎖
79+
Y.J. Lee下午8:21
80+
嗯 合理
81+
Y.J. Lee下午8:26
82+
嗯 他會動態調整memory
83+
Y.J. Lee下午8:33
84+
我們是用這個
85+
https://aws.amazon.com/kinesis/data-firehose/
86+
接好以後DB 的更新會自己propagate 到date warehouse
87+
data warehouse
88+
Y.J. Lee下午8:40
89+
那邊直接push也是可以啦 不過就while那邊要改一下
90+
Y.J. Lee下午8:42
91+
這題另外兩個做法就是 binary search tree 跟 sorted list 不確定swift有沒有library
92+
python sortedlist 可以 O(logN)加東西 O(1)取index
93+
他實作滿複雜的
94+
好像真的是很多個小list
95+
Y.J. Lee下午8:43
96+
對 不管怎麼加東西 他自己都是維持sorted
97+
bst要balance
98+
Y.J. Lee下午8:45
99+
對啊 就很複雜 不見得比較好
100+
Y.J. Lee下午8:47
101+
用不到 但是面試可能會考XD
102+
Y.J. Lee下午8:50
103+
類似 bucket sort
104+
都變 O(1)
105+
Y.J. Lee下午8:52
106+
固定 number of buckets = 100, O(100) 也算是常數
107+
Y.J. Lee下午8:56
108+
sliding window median其實真的很類似
109+
就maintain 一個 toRemove (set?)
110+
window 裡面一樣兩個heap
111+
我猜的 也沒寫過 XD
112+
Y.J. Lee下午9:01
113+
QQ
114+
Y.J. Lee下午9:02
115+
先撤拉 晚安~

0 commit comments

Comments
 (0)