Skip to content

Commit 6ab5092

Browse files
add 703
1 parent 81c9ec7 commit 6ab5092

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Your ideas/fixes/algorithms are more than welcome!
123123
|706|[Design HashMap](https://leetcode.com/problems/design-hashmap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_706.java) | O(n) | O(n) | |Easy| Design
124124
|705|[Design HashSet](https://leetcode.com/problems/design-hashset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_705.java) | O(1) | O(n) | |Easy| Design
125125
|704|[Binary Search](https://leetcode.com/problems/binary-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_704.java) | O(logn) | O(1) | |Easy| Binary Search
126+
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_703.java) | O(logn) | O(n) | |Easy|
126127
|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_701.java) | O(n) | O(h) | |Medium | DFS, recursion
127128
|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_700.java) | O(n) | O(h) | |Easy| recusion, dfs
128129
|699|[Falling Squares](https://leetcode.com/problems/falling-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_699.java) | O(n^2) | O(n) | |Hard | Segment Tree
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.PriorityQueue;
7+
8+
/**
9+
* 703. Kth Largest Element in a Stream
10+
*
11+
* Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
12+
* Your KthLargest class will have a constructor which accepts an integer K and an integer array nums,
13+
* which contains initial elements from the stream.
14+
* For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
15+
*
16+
* Example:
17+
*
18+
* int K = 3;
19+
* int[] arr = [4,5,8,2];
20+
* KthLargest kthLargest = new KthLargest(3, arr);
21+
* kthLargest.add(3); // returns 4
22+
* kthLargest.add(5); // returns 5
23+
* kthLargest.add(10); // returns 5
24+
* kthLargest.add(9); // returns 8
25+
* kthLargest.add(4); // returns 8
26+
* Note:
27+
* You may assume that nums' length ≥ K-1 and K ≥ 1.
28+
*/
29+
public class _703 {
30+
public static class Solution1 {
31+
public static class KthLargest {
32+
PriorityQueue<Integer> heap;
33+
int K;
34+
public KthLargest(int k, int[] nums) {
35+
heap = new PriorityQueue<>(Collections.reverseOrder());
36+
for (int num : nums) {
37+
heap.offer(num);
38+
}
39+
K = k;
40+
}
41+
42+
public int add(int val) {
43+
List<Integer> tmp = new ArrayList<>();
44+
int result = 0;
45+
int tmpK = K;
46+
heap.offer(val);
47+
while (tmpK-- > 0) {
48+
result = heap.poll();
49+
tmp.add(result);
50+
}
51+
for (int num : tmp) {
52+
heap.offer(num);
53+
}
54+
return result;
55+
}
56+
}
57+
}
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._703;
4+
import org.junit.Test;
5+
6+
import static junit.framework.Assert.assertEquals;
7+
8+
public class _703Test {
9+
private static _703.Solution1.KthLargest solution1;
10+
private static int[] A;
11+
12+
@Test
13+
public void test1() {
14+
solution1 = new _703.Solution1.KthLargest(3, new int[] {4, 5, 8, 2});
15+
assertEquals(4, solution1.add(3));
16+
assertEquals(5, solution1.add(5));
17+
assertEquals(5, solution1.add(10));
18+
assertEquals(8, solution1.add(9));
19+
assertEquals(8, solution1.add(4));
20+
}
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy