Skip to content

Commit 3c09fdc

Browse files
committed
Add solution 703
1 parent c816f77 commit 3c09fdc

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Complete solutions to Leetcode problems, updated daily.
3636
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/doocs/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
3737
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
3838
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
39+
| 703 | [Kth Largest Element in a Stream](%20ttps://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
3940
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
4041

4142

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## 数据流中的第K大元素
2+
### 题目描述
3+
4+
设计一个找到数据流中第 K 大元素的类(class)。注意是排序后的第 K 大元素,不是第 K 个不同的元素。
5+
6+
你的 `KthLargest` 类需要一个同时接收整数 `k` 和整数数组`nums` 的构造器,它包含数据流中的初始元素。每次调用 `KthLargest.add`,返回当前数据流中第 K 大的元素。
7+
8+
示例:
9+
```
10+
int k = 3;
11+
int[] arr = [4,5,8,2];
12+
KthLargest kthLargest = new KthLargest(3, arr);
13+
kthLargest.add(3); // returns 4
14+
kthLargest.add(5); // returns 5
15+
kthLargest.add(10); // returns 5
16+
kthLargest.add(9); // returns 8
17+
kthLargest.add(4); // returns 8
18+
```
19+
20+
说明:
21+
22+
你可以假设 `nums` 的长度≥ `k-1``k` ≥ 1。
23+
24+
### 解法
25+
建立一个有 k 个元素的小根堆。add 操作时:
26+
27+
- 若堆元素少于 k 个,直接添加到堆中;
28+
- 若堆元素有 k 个,判断堆顶元素 peek() 与 val 的大小关系:若 peek() >= val,直接返回堆顶元素;若 peek() < val,弹出堆顶元素,将 val 添加至堆中,然后返回堆顶。
29+
30+
```java
31+
class KthLargest {
32+
33+
PriorityQueue<Integer> queue;
34+
35+
private int size = 0;
36+
37+
public KthLargest(int k, int[] nums) {
38+
size = k;
39+
queue = new PriorityQueue<>(k, Integer::compareTo);
40+
int gap = nums.length - k;
41+
if (gap > 0) {
42+
for (int i = 0; i < k; ++i) {
43+
queue.offer(nums[i]);
44+
}
45+
for (int i = k; i < nums.length; ++i) {
46+
add(nums[i]);
47+
}
48+
} else {
49+
for (int i = 0; i < nums.length; ++i) {
50+
queue.offer(nums[i]);
51+
}
52+
}
53+
}
54+
55+
public int add(int val) {
56+
if (queue.size() < size) {
57+
queue.offer(val);
58+
return queue.peek();
59+
}
60+
61+
if (queue.peek() >= val) {
62+
return queue.peek();
63+
}
64+
65+
queue.poll();
66+
queue.offer(val);
67+
return queue.peek();
68+
69+
}
70+
}
71+
72+
/**
73+
* Your KthLargest object will be instantiated and called as such:
74+
* KthLargest obj = new KthLargest(k, nums);
75+
* int param_1 = obj.add(val);
76+
*/
77+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class KthLargest {
2+
3+
PriorityQueue<Integer> queue;
4+
5+
private int size = 0;
6+
7+
public KthLargest(int k, int[] nums) {
8+
size = k;
9+
queue = new PriorityQueue<>(k, Integer::compareTo);
10+
int gap = nums.length - k;
11+
if (gap > 0) {
12+
for (int i = 0; i < k; ++i) {
13+
queue.offer(nums[i]);
14+
}
15+
for (int i = k; i < nums.length; ++i) {
16+
add(nums[i]);
17+
}
18+
} else {
19+
for (int i = 0; i < nums.length; ++i) {
20+
queue.offer(nums[i]);
21+
}
22+
}
23+
}
24+
25+
public int add(int val) {
26+
if (queue.size() < size) {
27+
queue.offer(val);
28+
return queue.peek();
29+
}
30+
31+
if (queue.peek() >= val) {
32+
return queue.peek();
33+
}
34+
35+
queue.poll();
36+
queue.offer(val);
37+
return queue.peek();
38+
39+
}
40+
}
41+
42+
/**
43+
* Your KthLargest object will be instantiated and called as such:
44+
* KthLargest obj = new KthLargest(k, nums);
45+
* int param_1 = obj.add(val);
46+
*/

0 commit comments

Comments
 (0)