Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9bde29

Browse files
update 703
1 parent 8a6c4d1 commit a9bde29

File tree

3 files changed

+24
-29
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+24
-29
lines changed

‎paginated_contents/algorithms/1st_thousand/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_706.java) | | Easy | Design
171171
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_705.java) | | Easy | Design
172172
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_704.java) | [:tv:](https://youtu.be/eHVe_uyXeWg) | Easy | Binary Search
173-
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_703.java) | | Easy |
173+
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_703.java) | | Easy | PriorityQueue, Design
174174
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_701.java) | | Medium | DFS, recursion
175175
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_700.java) | | Easy | recusion, dfs
176176
| 699 | [Falling Squares](https://leetcode.com/problems/falling-squares/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_699.java) || Hard | Segment Tree

‎src/main/java/com/fishercoder/solutions/firstthousand/_703.java‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
package com.fishercoder.solutions.firstthousand;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.List;
63
import java.util.PriorityQueue;
74

85
public class _703 {
96
public static class Solution1 {
107
public static class KthLargest {
11-
PriorityQueue<Integer> heap;
8+
PriorityQueue<Integer> minHeap;
129
int maxK;
1310

1411
public KthLargest(int k, int[] nums) {
15-
heap = new PriorityQueue<>(Collections.reverseOrder());
12+
minHeap = new PriorityQueue<>();
1613
for (int num : nums) {
17-
heap.offer(num);
14+
minHeap.offer(num);
15+
if (minHeap.size() > k) {
16+
minHeap.poll();
17+
}
1818
}
1919
maxK = k;
2020
}
2121

2222
public int add(int val) {
23-
List<Integer> tmp = new ArrayList<>();
24-
int result = 0;
25-
int tmpK = maxK;
26-
heap.offer(val);
27-
while (tmpK-- > 0) {
28-
result = heap.poll();
29-
tmp.add(result);
23+
if (minHeap.size() < maxK || minHeap.peek() < val) {
24+
minHeap.offer(val);
25+
if (minHeap.size() > maxK) {
26+
minHeap.poll();
27+
}
3028
}
31-
for (int num : tmp) {
32-
heap.offer(num);
33-
}
34-
return result;
29+
return minHeap.peek();
3530
}
3631
}
3732
}

‎src/test/java/com/fishercoder/firstthousand/_703Test.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
import static org.junit.jupiter.api.Assertions.assertEquals;
77

88
public class _703Test {
9-
private _703.Solution1.KthLargest solution1;
10-
private static int[] A;
9+
private _703.Solution1.KthLargest solution1;
10+
private static int[] A;
1111

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-
}
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+
}
2121
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /