Movatterモバイル変換


[0]ホーム

URL:


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

Commita9bde29

Browse files
update 703
1 parent8a6c4d1 commita9bde29

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
packagecom.fishercoder.solutions.firstthousand;
22

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

85
publicclass_703 {
96
publicstaticclassSolution1 {
107
publicstaticclassKthLargest {
11-
PriorityQueue<Integer>heap;
8+
PriorityQueue<Integer>minHeap;
129
intmaxK;
1310

1411
publicKthLargest(intk,int[]nums) {
15-
heap =newPriorityQueue<>(Collections.reverseOrder());
12+
minHeap =newPriorityQueue<>();
1613
for (intnum :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
publicintadd(intval) {
23-
List<Integer>tmp =newArrayList<>();
24-
intresult =0;
25-
inttmpK =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 (intnum :tmp) {
32-
heap.offer(num);
33-
}
34-
returnresult;
29+
returnminHeap.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
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
77

88
publicclass_703Test {
9-
private_703.Solution1.KthLargestsolution1;
10-
privatestaticint[]A;
9+
private_703.Solution1.KthLargestsolution1;
10+
privatestaticint[]A;
1111

12-
@Test
13-
publicvoidtest1() {
14-
solution1 =new_703.Solution1.KthLargest(3,newint[]{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+
publicvoidtest1() {
14+
solution1 =new_703.Solution1.KthLargest(3,newint[]{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)

[8]ページ先頭

©2009-2025 Movatter.jp