Skip to content

Commit d34038a

Browse files
committed
Create README - LeetHub
1 parent 1567aa4 commit d34038a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Diff for: find-median-from-data-stream/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<h2>295. Find Median from Data Stream</h2><h3>Hard</h3><hr><div><p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.</p>
2+
3+
<ul>
4+
<li>For example, for <code>arr = [2,3,4]</code>, the median is <code>3</code>.</li>
5+
<li>For example, for <code>arr = [2,3]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li>
6+
</ul>
7+
8+
<p>Implement the MedianFinder class:</p>
9+
10+
<ul>
11+
<li><code>MedianFinder()</code> initializes the <code>MedianFinder</code> object.</li>
12+
<li><code>void addNum(int num)</code> adds the integer <code>num</code> from the data stream to the data structure.</li>
13+
<li><code>double findMedian()</code> returns the median of all elements so far. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</li>
14+
</ul>
15+
16+
<p>&nbsp;</p>
17+
<p><strong>Example 1:</strong></p>
18+
19+
<pre><strong>Input</strong>
20+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
21+
[[], [1], [2], [], [3], []]
22+
<strong>Output</strong>
23+
[null, null, null, 1.5, null, 2.0]
24+
25+
<strong>Explanation</strong>
26+
MedianFinder medianFinder = new MedianFinder();
27+
medianFinder.addNum(1); // arr = [1]
28+
medianFinder.addNum(2); // arr = [1, 2]
29+
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
30+
medianFinder.addNum(3); // arr[1, 2, 3]
31+
medianFinder.findMedian(); // return 2.0
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>-10<sup>5</sup> &lt;= num &lt;= 10<sup>5</sup></code></li>
39+
<li>There will be at least one element in the data structure before calling <code>findMedian</code>.</li>
40+
<li>At most <code>5 * 10<sup>4</sup></code> calls will be made to <code>addNum</code> and <code>findMedian</code>.</li>
41+
</ul>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Follow up:</strong></p>
45+
46+
<ul>
47+
<li>If all integer numbers from the stream are in the range <code>[0, 100]</code>, how would you optimize your solution?</li>
48+
<li>If <code>99%</code> of all integer numbers from the stream are in the range <code>[0, 100]</code>, how would you optimize your solution?</li>
49+
</ul>
50+
</div>

0 commit comments

Comments
 (0)