Skip to content

Commit ecc018e

Browse files
committed
一刷295
1 parent 4b88f39 commit ecc018e

19 files changed

+105
-9
lines changed

README.adoc

+8-8
Original file line numberDiff line numberDiff line change
@@ -2090,14 +2090,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
20902090
//|{doc_base_url}/0294-flip-game-ii.adoc[题解]
20912091
//|Medium
20922092
//|
2093-
//
2094-
//|{counter:codes}
2095-
//|{leetcode_base_url}/find-median-from-data-stream/[295. Find Median from Data Stream^]
2096-
//|{source_base_url}/_0295_FindMedianFromDataStream.java[Java]
2097-
//|{doc_base_url}/0295-find-median-from-data-stream.adoc[题解]
2098-
//|Hard
2099-
//|
2100-
//
2093+
2094+
|{counter:codes}
2095+
|{leetcode_base_url}/find-median-from-data-stream/[295. Find Median from Data Stream^]
2096+
|{source_base_url}/_0295_FindMedianFromDataStream.java[Java]
2097+
|{doc_base_url}/0295-find-median-from-data-stream.adoc[题解]
2098+
|Hard
2099+
|
2100+
21012101
//|{counter:codes}
21022102
//|{leetcode_base_url}/best-meeting-point/[296. Best Meeting Point^]
21032103
//|{source_base_url}/_0296_BestMeetingPoint.java[Java]

docs/0295-find-median-from-data-stream.adoc

+51
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,63 @@ findMedian() -> 2
3838
* If all integer numbers from the stream are between 0 and 100, how would you optimize it?
3939
* If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
4040

41+
== 思路分析
4142

43+
image::images/0295-01.png[{image_attr}]
44+
45+
image::images/0295-02.png[{image_attr}]
46+
47+
image::images/0295-03.png[{image_attr}]
48+
49+
image::images/0295-04.png[{image_attr}]
50+
51+
image::images/0295-05.png[{image_attr}]
52+
53+
image::images/0295-06.png[{image_attr}]
54+
55+
image::images/0295-07.png[{image_attr}]
56+
57+
image::images/0295-08.png[{image_attr}]
58+
59+
image::images/0295-09.png[{image_attr}]
60+
61+
image::images/0295-10.png[{image_attr}]
62+
63+
image::images/0295-11.png[{image_attr}]
64+
65+
image::images/0295-12.png[{image_attr}]
66+
67+
image::images/0295-13.png[{image_attr}]
68+
69+
image::images/0295-14.png[{image_attr}]
4270

4371

4472
[[src-0295]]
73+
[tabs]
74+
====
75+
一刷::
76+
+
77+
--
4578
[{java_src_attr}]
4679
----
4780
include::{sourcedir}/_0295_FindMedianFromDataStream.java[tag=answer]
4881
----
82+
--
83+
84+
// 二刷::
85+
// +
86+
// --
87+
// [{java_src_attr}]
88+
// ----
89+
// include::{sourcedir}/_0005_LongestPalindromicSubstring_2.java[tag=answer]
90+
// ----
91+
// --
92+
====
93+
94+
== 参考资料
95+
96+
. https://leetcode.cn/problems/find-median-from-data-stream/solutions/961062/shu-ju-liu-de-zhong-wei-shu-by-leetcode-ktkst/[295. 数据流的中位数 - 官方题解^]
97+
. https://leetcode.cn/problems/find-median-from-data-stream/solutions/2361972/295-shu-ju-liu-de-zhong-wei-shu-dui-qing-gmdo/[295. 数据流的中位数 - 堆,清晰图解^]
98+
99+
49100

docs/images/0295-01.png

25.5 KB
Loading

docs/images/0295-02.png

9.89 KB
Loading

docs/images/0295-03.png

12.9 KB
Loading

docs/images/0295-04.png

13.3 KB
Loading

docs/images/0295-05.png

13.4 KB
Loading

docs/images/0295-06.png

14.8 KB
Loading

docs/images/0295-07.png

13.6 KB
Loading

docs/images/0295-08.png

13.9 KB
Loading

docs/images/0295-09.png

14.1 KB
Loading

docs/images/0295-10.png

15.7 KB
Loading

docs/images/0295-11.png

14.2 KB
Loading

docs/images/0295-12.png

14.6 KB
Loading

docs/images/0295-13.png

14.7 KB
Loading

docs/images/0295-14.png

16.3 KB
Loading

docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ include::0289-game-of-life.adoc[leveloffset=+1]
664664

665665
// include::0294-flip-game-ii.adoc[leveloffset=+1]
666666

667-
// include::0295-find-median-from-data-stream.adoc[leveloffset=+1]
667+
include::0295-find-median-from-data-stream.adoc[leveloffset=+1]
668668

669669
// include::0296-best-meeting-point.adoc[leveloffset=+1]
670670

logbook/202406.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@
424424
|{doc_base_url}/0268-missing-number.adoc[题解]
425425
|循环排序
426426

427+
|{counter:codes}
428+
|{leetcode_base_url}/find-median-from-data-stream/[295. Find Median from Data Stream^]
429+
|{doc_base_url}/0295-find-median-from-data-stream.adoc[题解]
430+
|双堆思路太妙了!
431+
427432
|===
428433

429434
截止目前,本轮练习一共完成 {codes} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.Queue;
5+
6+
public class _0295_FindMedianFromDataStream {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-08-29 20:43:20
11+
*/
12+
class MedianFinder {
13+
Queue<Integer> big;
14+
Queue<Integer> small;
15+
16+
public MedianFinder() {
17+
// 小顶堆,顶部是最小的。保存较大的一半
18+
big = new PriorityQueue<>();
19+
// 大顶堆,顶部是最大的。保存较小的一半
20+
small = new PriorityQueue<>((a, b) -> b - a);
21+
}
22+
23+
public void addNum(int num) {
24+
if (big.size() != small.size()) {
25+
// 倒腾一下,实际 small 中的元素增加了
26+
big.add(num);
27+
small.add(big.poll());
28+
} else {
29+
// 倒腾一下,实际 big 中的元素增加了
30+
small.add(num);
31+
big.add(small.poll());
32+
}
33+
}
34+
35+
public double findMedian() {
36+
return big.size() != small.size() ? big.peek() : (big.peek() + small.peek()) / 2.0;
37+
}
38+
}
39+
// end::answer[]
40+
}

0 commit comments

Comments
 (0)