File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
find-median-from-data-stream Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ class MedianFinder :
2+ # Follow up : λ κ°μ heap μ¬μ©ν΄μ ꡬν κ°λ₯(μκ°λ³΅μ‘λ O(log n), μΆκ° κ³΅λΆ νμν¨)
3+ # heap μ¬μ©νμ§ μκ³ μ΄μ§ νμ μ½μ
μΌλ‘ νμμ
4+
5+ def __init__ (self ):
6+ # μ«μ μ μ₯ν 리μ€νΈ μμ±
7+ self .arr = []
8+
9+
10+ def addNum (self , num : int ) -> None :
11+ # μ«μ μΆκ°νκ³ μ λ ¬νκΈ°
12+ # self.arr.append(num)
13+ # self.arr.sort() -> μκ°μ΄κ³Ό(μκ°λ³΅μ‘λ O(n log n))
14+ # μ΄μ§νμμ½μ
(μκ°λ³΅μ‘λ O(n))
15+ bisect .insort (self .arr , num )
16+
17+ def findMedian (self ) -> float :
18+ # κΈΈμ΄κ° νμλ©΄ κ°μ΄λ° κ° λ¦¬ν΄
19+ if len (self .arr )% 2 == 1 :
20+ return self .arr [len (self .arr )// 2 ]
21+ # κΈΈμ΄κ° μ§μλ©΄ κ°μ΄λ° λ μμ νκ· λ¦¬ν΄
22+ return (self .arr [len (self .arr )// 2 - 1 ] + self .arr [len (self .arr )// 2 ]) / 2
23+
24+ # Your MedianFinder object will be instantiated and called as such:
25+ # obj = MedianFinder()
26+ # obj.addNum(num)
27+ # param_2 = obj.findMedian()
You canβt perform that action at this time.
0 commit comments