Skip to content

Commit 600d7aa

Browse files
committed
fd
1 parent 42d155d commit 600d7aa

File tree

5 files changed

+56
-57
lines changed

5 files changed

+56
-57
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@
388388
|681|[Next Closest Time](https://leetcode.com/problems/next-closest-time/)|[Java](leetcode/solution/src/NextClosestTime.java)|80|这题多做几遍|
389389
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)| [Java](leetcode/solution/src/RedundantConnection.java)|||
390390
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Java](leetcode/solution/src/MaxAreaOfIsland.java)|100||
391+
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Java](leetcode/solution/src/KthLargestElementInAStream.java)|100||
391392
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
392393
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
393394
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Queue;
3+
4+
public class KthLargestElementInAStream {
5+
6+
class KthLargest {
7+
8+
Queue<Integer> queue = new PriorityQueue<>();
9+
int capacity;
10+
11+
public KthLargest(int k, int[] nums) {
12+
capacity = k;
13+
for (int n : nums) {
14+
queue.offer(n);
15+
}
16+
}
17+
18+
public int add(int val) {
19+
queue.offer(val);
20+
while (queue.size() > capacity) {
21+
queue.poll();
22+
}
23+
return queue.peek();
24+
}
25+
}
26+
}

leetcode/solution/src/KthLargestElementInArray.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import java.util.PriorityQueue;
33
import java.util.Random;
44

5+
/**
6+
* 注意本题不是返回第k大的distinct number
7+
*/
58
public class KthLargestElementInArray {
69

710
// 耗时15ms,时间复杂度O(nlgk),空间复杂度O(k)

leetcode/solution/src/ThirdMaximumNumber.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,24 @@
44
public class ThirdMaximumNumber {
55

66
public int thirdMax(int[] nums) {
7-
int count = 0;
8-
97
long first = (long) Integer.MIN_VALUE - 1;
10-
long second = (long) Integer.MIN_VALUE - 1;
11-
long third = (long) Integer.MIN_VALUE - 1;
8+
long second = first, third = first;
129

13-
for (int n : nums) {
14-
if (n == first || n == second || n == third) {
10+
for (int i = 0; i < nums.length; i++) {
11+
if (nums[i] == first || nums[i] == second || nums[i] == third) {
1512
continue;
1613
}
17-
18-
if (n > first) {
19-
count++;
14+
if (nums[i] > first) {
2015
third = second;
2116
second = first;
22-
first = n;
23-
} else if (n > second) {
24-
count++;
17+
first = nums[i];
18+
} else if (nums[i] > second) {
2519
third = second;
26-
second = n;
27-
} else if (n >= third) {
28-
count++;
29-
third = n;
20+
second = nums[i];
21+
} else if (nums[i] > third) {
22+
third = nums[i];
3023
}
3124
}
32-
33-
return (int) (count >= 3 ? third : first);
25+
return (int) (third >= Integer.MIN_VALUE ? third : first);
3426
}
3527
}

leetcode/src/Main.java

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,31 @@ public class Main {
44

55
public static class Solution {
66

7-
public int findKthLargest(int[] nums, int k) {
8-
shuffle(nums);
9-
return findKthLargest(nums, 0, nums.length - 1, k);
10-
}
117

12-
private void shuffle(int[] nums) {
13-
Random rnd = new Random(System.currentTimeMillis());
14-
for (int i = nums.length - 1; i > 1; i--) {
15-
int index = rnd.nextInt(i + 1);
16-
swap(nums, index, i);
17-
}
18-
}
198

20-
public int findKthLargest(int[] nums, int start, int end, int k) {
21-
int index = partition(nums, start, end);
22-
int count = end - index + 1;
23-
if (count < k) {
24-
return findKthLargest(nums, start, index - 1, k - count);
25-
} else if (count > k) {
26-
return findKthLargest(nums, index + 1, end, k);
27-
} else {
28-
return nums[index];
29-
}
30-
}
9+
}
10+
11+
class KthLargest {
12+
13+
Queue<Integer> queue = new PriorityQueue<>();
14+
int capacity;
3115

32-
private int partition(int[] nums, int start, int end) {
33-
int left = start, right = end -1, pivot = nums[end];
34-
for (int i = start; i < right; ) {
35-
if (nums[i] < pivot) {
36-
swap(nums, left++, i++);
37-
} else if (nums[i] > pivot) {
38-
swap(nums, right--, i);
39-
} else {
40-
i++;
41-
}
16+
public KthLargest(int k, int[] nums) {
17+
capacity = k;
18+
for (int n : nums) {
19+
queue.offer(n);
4220
}
43-
swap(nums, left, end);
44-
return left;
4521
}
4622

47-
private void swap(int[] nums, int i, int j) {
48-
int t = nums[i];
49-
nums[i] = nums[j];
50-
nums[j] = t;
23+
public int add(int val) {
24+
queue.offer(val);
25+
while (queue.size() > capacity) {
26+
queue.poll();
27+
}
28+
return queue.peek();
5129
}
5230
}
5331

54-
5532
public static void main(String[] args) {
5633
Solution solution = new Solution();
5734
}

0 commit comments

Comments
 (0)