Skip to content

Commit 42d155d

Browse files
committed
fd
1 parent 845fc37 commit 42d155d

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

leetcode/solution/src/KthLargestElementInArray.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.Arrays;
22
import java.util.PriorityQueue;
3+
import java.util.Random;
34

45
public class KthLargestElementInArray {
56

@@ -26,11 +27,23 @@ public int findKthLargest3(int[] nums, int k) {
2627
// T(n) = T(n / 2) + n = O(2n)
2728
// 对比快速排序T(n) = 2T(n / 2) + n = O(nlgn)
2829
// 区别在于这个被pivot分隔后,只用处理其中的一半,而快排两边都要处理
29-
// 耗时18ms
30+
31+
/**
32+
* shuffle非常有必要,性能提升很大
33+
*/
3034
public int findKthLargest(int[] nums, int k) {
35+
shuffle(nums);
3136
return findKthLargest(nums, 0, nums.length - 1, k);
3237
}
3338

39+
private void shuffle(int[] nums) {
40+
Random rnd = new Random(System.currentTimeMillis());
41+
for (int i = nums.length - 1; i > 1; i--) {
42+
int index = rnd.nextInt(i + 1);
43+
swap(nums, index, i);
44+
}
45+
}
46+
3447
public int findKthLargest(int[] nums, int start, int end, int k) {
3548
int pivot = partition(nums, start, end);
3649

@@ -45,6 +58,10 @@ public int findKthLargest(int[] nums, int start, int end, int k) {
4558
}
4659
}
4760

61+
/**
62+
* 这里两端都是闭区间
63+
* 这里将数组分为两部分,左边小于pivot,右边大于pivot,中间可能等于pivot
64+
*/
4865
public int partition(int[] nums, int start, int end) {
4966
int pivot = nums[end], left = start, right = end - 1;
5067

leetcode/src/Main.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,50 @@ public class Main {
44

55
public static class Solution {
66

7-
class MinStack {
8-
9-
/** initialize your data structure here. */
10-
public MinStack() {
11-
12-
}
13-
14-
public void push(int x) {
7+
public int findKthLargest(int[] nums, int k) {
8+
shuffle(nums);
9+
return findKthLargest(nums, 0, nums.length - 1, k);
10+
}
1511

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);
1617
}
18+
}
1719

18-
public void pop() {
19-
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];
2029
}
30+
}
2131

22-
public int top() {
23-
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+
}
2442
}
43+
swap(nums, left, end);
44+
return left;
45+
}
2546

26-
public int getMin() {
27-
28-
}
47+
private void swap(int[] nums, int i, int j) {
48+
int t = nums[i];
49+
nums[i] = nums[j];
50+
nums[j] = t;
2951
}
3052
}
3153

0 commit comments

Comments
 (0)