Skip to content

Commit f6407e1

Browse files
authored
Update sliding-window-median.py
1 parent 8cbcf1d commit f6407e1

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

Python/sliding-window-median.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ def medianSlidingWindow(self, nums, k):
3333
:type k: int
3434
:rtype: List[float]
3535
"""
36-
def rebuild_heap(heap, to_remove, sign): # Time: O(k), Space: O(k)
36+
def lazy_delete(heap, to_remove, sign):
37+
while heap and sign*heap[0] in to_remove:
38+
to_remove[sign*heap[0]] -= 1
39+
if not to_remove[sign*heap[0]]:
40+
del to_remove[sign*heap[0]]
41+
heapq.heappop(heap)
42+
43+
def full_delete(heap, to_remove, sign): # Time: O(k), Space: O(k)
3744
result = []
3845
for x in heap:
3946
if sign*x not in to_remove:
@@ -58,19 +65,11 @@ def rebuild_heap(heap, to_remove, sign): # Time: O(k), Space: O(k)
5865
if nums[i-k] > -max_heap[0]:
5966
heapq.heappush(min_heap, -heapq.heappop(max_heap))
6067
to_remove[nums[i-k]] += 1
61-
while max_heap and -max_heap[0] in to_remove: # lazy delete
62-
to_remove[-max_heap[0]] -= 1
63-
if not to_remove[-max_heap[0]]:
64-
del to_remove[-max_heap[0]]
65-
heapq.heappop(max_heap)
66-
while min_heap[0] in to_remove:
67-
to_remove[min_heap[0]] -= 1
68-
if not to_remove[min_heap[0]]:
69-
del to_remove[min_heap[0]]
70-
heapq.heappop(min_heap)
68+
lazy_delete(max_heap, to_remove, -1)
69+
lazy_delete(min_heap, to_remove, 1)
7170
if len(min_heap)+len(max_heap) > 2*k: # full delete
72-
rebuild_heap(max_heap, to_remove, -1)
73-
rebuild_heap(min_heap, to_remove, 1)
71+
full_delete(max_heap, to_remove, -1)
72+
full_delete(min_heap, to_remove, 1)
7473
result.append(float(min_heap[0]) if k%2 else (min_heap[0]-max_heap[0])/2.0)
7574
return result
7675

@@ -88,6 +87,13 @@ def medianSlidingWindow(self, nums, k):
8887
:type k: int
8988
:rtype: List[float]
9089
"""
90+
def lazy_delete(heap, to_remove, sign):
91+
while heap and sign*heap[0] in to_remove:
92+
to_remove[sign*heap[0]] -= 1
93+
if not to_remove[sign*heap[0]]:
94+
del to_remove[sign*heap[0]]
95+
heapq.heappop(heap)
96+
9197
min_heap, max_heap = [], []
9298
for i in xrange(k):
9399
if i%2 == 0:
@@ -101,15 +107,7 @@ def medianSlidingWindow(self, nums, k):
101107
if nums[i-k] > -max_heap[0]:
102108
heapq.heappush(min_heap, -heapq.heappop(max_heap))
103109
to_remove[nums[i-k]] += 1
104-
while max_heap and -max_heap[0] in to_remove: # lazy delete
105-
to_remove[-max_heap[0]] -= 1
106-
if not to_remove[-max_heap[0]]:
107-
del to_remove[-max_heap[0]]
108-
heapq.heappop(max_heap)
109-
while min_heap[0] in to_remove:
110-
to_remove[min_heap[0]] -= 1
111-
if not to_remove[min_heap[0]]:
112-
del to_remove[min_heap[0]]
113-
heapq.heappop(min_heap)
110+
lazy_delete(max_heap, to_remove, -1)
111+
lazy_delete(min_heap, to_remove, 1)
114112
result.append(float(min_heap[0]) if k%2 else (min_heap[0]-max_heap[0])/2.0)
115113
return result

0 commit comments

Comments
 (0)