Skip to content

Commit e68eec6

Browse files
committed
Leetcode TLE with python 2. It accepts with python 3.
1 parent 484e509 commit e68eec6

2 files changed

Lines changed: 84 additions & 3 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Find Median from Data Stream
2+
# https://leetcode.com/problems/find-median-from-data-stream/
3+
4+
class MedianFinder(object):
5+
6+
def __init__(self):
7+
# holds all elements greater than median
8+
self.minHeap = []
9+
# holds all elements smaller than median.
10+
self.maxHeap = []
11+
"""
12+
initialize your data structure here.
13+
"""
14+
15+
def addToMinHeap(self, num):
16+
# add number to min heap
17+
heapq.heappush(self.minHeap, num)
18+
# if length of heap is more then pop the element and add to max heap
19+
if (len(self.minHeap) > len(self.maxHeap)+1):
20+
element = heapq.heappop(self.minHeap)
21+
self.addToMaxHeap(element)
22+
23+
def addToMaxHeap(self, num):
24+
# add number to max heap
25+
self.maxHeap.append(num)
26+
heapq._heapify_max(self.maxHeap)
27+
# if length of heap is more then pop the item and add to min Heap
28+
if (len(self.maxHeap) > len(self.minHeap)+1):
29+
element = self.maxHeap.pop(0)
30+
heapq._heapify_max(self.maxHeap)
31+
self.addToMinHeap(element)
32+
33+
def addNum(self, num):
34+
# if both heaps are empty
35+
if ((not self.minHeap) and (not self.maxHeap)):
36+
self.addToMinHeap(num)
37+
return
38+
# if max heap is empty but min heap is not.
39+
if ((not self.maxHeap) and self.minHeap):
40+
# if top of min heap < num (because min heap needs to hold elements greater than median)
41+
if (self.minHeap[0] < num):
42+
# pop the top from min heap and add to max heap
43+
topMinElement = heapq.heappop(self.minHeap)
44+
self.addToMaxHeap(topMinElement)
45+
# add current element to min heap
46+
self.addToMinHeap(num)
47+
return
48+
# if both heaps have elements
49+
if (self.minHeap and self.maxHeap):
50+
# max heap contains elements from 0 to median
51+
if (num < self.maxHeap[0]):
52+
self.addToMaxHeap(num)
53+
else:
54+
self.addToMinHeap(num)
55+
"""
56+
:type num: int
57+
:rtype: None
58+
"""
59+
60+
def findMedian(self):
61+
if (len(self.minHeap) == len(self.maxHeap)):
62+
return float(self.minHeap[0]+self.maxHeap[0])/float(2)
63+
if (len(self.minHeap) > len(self.maxHeap)):
64+
return self.minHeap[0]
65+
return self.maxHeap[0]
66+
"""
67+
:rtype: float
68+
"""
69+
70+
71+
72+
# Your MedianFinder object will be instantiated and called as such:
73+
# obj = MedianFinder()
74+
# obj.addNum(num)
75+
# param_2 = obj.findMedian()

Blind 75/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1>Blind 75</h1>
22
<a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions">Leetcode link to the post.</a>
3-
<h4>Total Completed: 29</h4>
3+
<h4>Total Completed: 31</h4>
44
<h2>Array (Solved: 4, Unsolved: 6)</h2>
55
<ul>
66
<li><a href="Programs/Two Sum.py">Two Sum</a>
@@ -284,7 +284,7 @@
284284
<li>Add and Search Word</li>
285285
<li>Word Search II</li>
286286
</ul>
287-
<h2>Heap (Solved: 1, Unsolved: 2)</h2>
287+
<h2>Heap (Solved: 2, Unsolved: 1)</h2>
288288
<ul>
289289
<li><a href="Programs/Merge K sorted Lists.py">Merge K Sorted Lists</a> </li>
290290
<li><a href="Programs/Top K Frequent Elements.py">Top K Frequent Elements</a>
@@ -293,7 +293,13 @@
293293
Remove the top k elements from the heap.
294294
</p>
295295
</li>
296-
<li>Find Median from Data Stream</li>
296+
<li><a href="Programs/Find Median from Data Stream.py">Find Median from Data Stream</a>
297+
<p><b>Approach</b>: Keep 2 heaps, max heap to hold all elements less than median and min heap to hold elements greater than the median. <br/>
298+
Finding median: which ever heap has more elements, the top element of that heap is the median. <br/>
299+
If both heaps have same elements, then median is (minHeapTop+maxHeapTop) / 2 <br/>
300+
If at any point one heap has 2 elements more than other heap, pop an element from that heap and add it to another heap.
301+
</p>
302+
</li>
297303
</ul>
298304

299305

0 commit comments

Comments
 (0)