Skip to content

Commit 71b09c5

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.0215 (#3176)
Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent dfe1210 commit 71b09c5

20 files changed

+999
-388
lines changed

Diff for: solution/0200-0299/0215.Kth Largest Element in an Array/README.md

+356-130
Large diffs are not rendered by default.

Diff for: solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md

+356-130
Large diffs are not rendered by default.

Diff for: solution/0200-0299/0215.Kth Largest Element in an Array/Solution.cpp

+23-16
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@ class Solution {
22
public:
33
int findKthLargest(vector<int>& nums, int k) {
44
int n = nums.size();
5-
return quickSort(nums, 0, n - 1, n - k);
5+
k = n - k;
6+
auto quickSort = [&](auto&& quickSort, int l, int r) -> int {
7+
if (l == r) {
8+
return nums[l];
9+
}
10+
int i = l - 1, j = r + 1;
11+
int x = nums[(l + r) >> 1];
12+
while (i < j) {
13+
while (nums[++i] < x) {
14+
}
15+
while (nums[--j] > x) {
16+
}
17+
if (i < j) {
18+
swap(nums[i], nums[j]);
19+
}
20+
}
21+
if (j < k) {
22+
return quickSort(quickSort, j + 1, r);
23+
}
24+
return quickSort(quickSort, l, j);
25+
};
26+
return quickSort(quickSort, 0, n - 1);
627
}
7-
8-
int quickSort(vector<int>& nums, int left, int right, int k) {
9-
if (left == right) return nums[left];
10-
int i = left - 1, j = right + 1;
11-
int x = nums[left + right >> 1];
12-
while (i < j) {
13-
while (nums[++i] < x)
14-
;
15-
while (nums[--j] > x)
16-
;
17-
if (i < j) swap(nums[i], nums[j]);
18-
}
19-
return j < k ? quickSort(nums, j + 1, right, k) : quickSort(nums, left, j, k);
20-
}
21-
};
28+
};
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
func findKthLargest(nums []int, k int) int {
2-
n := len(nums)
3-
return quickSort(nums, 0, n-1, n-k)
4-
}
5-
6-
func quickSort(nums []int, left, right, k int) int {
7-
if left == right {
8-
return nums[left]
9-
}
10-
i, j := left-1, right+1
11-
x := nums[(left+right)>>1]
12-
for i < j {
13-
for {
14-
i++
15-
if nums[i] >= x {
16-
break
17-
}
2+
k = len(nums) - k
3+
var quickSort func(l, r int) int
4+
quickSort = func(l, r int) int {
5+
if l == r {
6+
return nums[l]
187
}
19-
for {
20-
j--
21-
if nums[j] <= x {
22-
break
8+
i, j := l-1, r+1
9+
x := nums[(l+r)>>1]
10+
for i < j {
11+
for {
12+
i++
13+
if nums[i] >= x {
14+
break
15+
}
16+
}
17+
for {
18+
j--
19+
if nums[j] <= x {
20+
break
21+
}
22+
}
23+
if i < j {
24+
nums[i], nums[j] = nums[j], nums[i]
2325
}
2426
}
25-
if i < j {
26-
nums[i], nums[j] = nums[j], nums[i]
27+
if j < k {
28+
return quickSort(j+1, r)
2729
}
30+
return quickSort(l, j)
2831
}
29-
if j < k {
30-
return quickSort(nums, j+1, right, k)
31-
}
32-
return quickSort(nums, left, j, k)
33-
}
32+
return quickSort(0, len(nums)-1)
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
class Solution {
2+
private int[] nums;
3+
private int k;
4+
25
public int findKthLargest(int[] nums, int k) {
3-
int n = nums.length;
4-
return quickSort(nums, 0, n - 1, n - k);
6+
this.nums = nums;
7+
this.k = nums.length - k;
8+
return quickSort(0, nums.length - 1);
59
}
610

7-
private int quickSort(int[] nums, int left, int right, int k) {
8-
if (left == right) {
9-
return nums[left];
11+
private int quickSort(int l, int r) {
12+
if (l == r) {
13+
return nums[l];
1014
}
11-
int i = left - 1, j = right + 1;
12-
int x = nums[(left + right) >>> 1];
15+
int i = l - 1, j = r + 1;
16+
int x = nums[(l + r) >>> 1];
1317
while (i < j) {
14-
while (nums[++i] < x)
15-
;
16-
while (nums[--j] > x)
17-
;
18+
while (nums[++i] < x) {
19+
}
20+
while (nums[--j] > x) {
21+
}
1822
if (i < j) {
1923
int t = nums[i];
2024
nums[i] = nums[j];
2125
nums[j] = t;
2226
}
2327
}
2428
if (j < k) {
25-
return quickSort(nums, j + 1, right, k);
29+
return quickSort(j + 1, r);
2630
}
27-
return quickSort(nums, left, j, k);
31+
return quickSort(l, j);
2832
}
29-
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
def findKthLargest(self, nums: List[int], k: int) -> int:
3-
def quick_sort(left, right, k):
4-
if left == right:
5-
return nums[left]
6-
i, j = left - 1, right + 1
7-
x = nums[(left + right) >> 1]
3+
def quick_sort(l: int, r: int) -> int:
4+
if l == r:
5+
return nums[l]
6+
i, j = l - 1, r + 1
7+
x = nums[(l + r) >> 1]
88
while i < j:
99
while 1:
1010
i += 1
@@ -17,8 +17,9 @@ def quick_sort(left, right, k):
1717
if i < j:
1818
nums[i], nums[j] = nums[j], nums[i]
1919
if j < k:
20-
return quick_sort(j + 1, right, k)
21-
return quick_sort(left, j, k)
20+
return quick_sort(j + 1, r)
21+
return quick_sort(l, j)
2222

2323
n = len(nums)
24-
return quick_sort(0, n - 1, n - k)
24+
k = n - k
25+
return quick_sort(0, n - 1)
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
use rand::Rng;
2-
31
impl Solution {
4-
fn sort(nums: &mut Vec<i32>, l: usize, r: usize, k: usize) {
5-
if l + 1 > k || l >= r {
6-
return;
2+
pub fn find_kth_largest(mut nums: Vec<i32>, k: i32) -> i32 {
3+
let len = nums.len();
4+
let k = len - k as usize;
5+
Self::quick_sort(&mut nums, 0, len - 1, k)
6+
}
7+
8+
fn quick_sort(nums: &mut Vec<i32>, l: usize, r: usize, k: usize) -> i32 {
9+
if l == r {
10+
return nums[l];
711
}
8-
nums.swap(l, rand::thread_rng().gen_range(l, r));
9-
let num = nums[l];
10-
let mut mark = l;
11-
for i in l..r {
12-
if nums[i] > num {
13-
mark += 1;
14-
nums.swap(i, mark);
12+
13+
let (mut i, mut j) = (l as isize - 1, r as isize + 1);
14+
let x = nums[(l + r) / 2];
15+
16+
while i < j {
17+
i += 1;
18+
while nums[i as usize] < x {
19+
i += 1;
1520
}
16-
}
17-
nums.swap(l, mark);
1821

19-
Self::sort(nums, l, mark, k);
20-
Self::sort(nums, mark + 1, r, k);
21-
}
22+
j -= 1;
23+
while nums[j as usize] > x {
24+
j -= 1;
25+
}
2226

23-
pub fn find_kth_largest(mut nums: Vec<i32>, k: i32) -> i32 {
24-
let n = nums.len();
25-
let k = k as usize;
26-
Self::sort(&mut nums, 0, n, k);
27-
nums[k - 1]
27+
if i < j {
28+
nums.swap(i as usize, j as usize);
29+
}
30+
}
31+
32+
let j = j as usize;
33+
if j < k {
34+
Self::quick_sort(nums, j + 1, r, k)
35+
} else {
36+
Self::quick_sort(nums, l, j, k)
37+
}
2838
}
2939
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
function findKthLargest(nums: number[], k: number): number {
22
const n = nums.length;
3-
const swap = (i: number, j: number) => {
4-
[nums[i], nums[j]] = [nums[j], nums[i]];
5-
};
6-
const sort = (l: number, r: number) => {
7-
if (l + 1 > k || l >= r) {
8-
return;
3+
k = n - k;
4+
const quickSort = (l: number, r: number): number => {
5+
if (l === r) {
6+
return nums[l];
97
}
10-
swap(l, l + Math.floor(Math.random() * (r - l)));
11-
const num = nums[l];
12-
let mark = l;
13-
for (let i = l + 1; i < r; i++) {
14-
if (nums[i] > num) {
15-
mark++;
16-
swap(i, mark);
8+
let [i, j] = [l - 1, r + 1];
9+
const x = nums[(l + r) >> 1];
10+
while (i < j) {
11+
while (nums[++i] < x);
12+
while (nums[--j] > x);
13+
if (i < j) {
14+
[nums[i], nums[j]] = [nums[j], nums[i]];
1715
}
1816
}
19-
swap(l, mark);
20-
21-
sort(l, mark);
22-
sort(mark + 1, r);
17+
if (j < k) {
18+
return quickSort(j + 1, r);
19+
}
20+
return quickSort(l, j);
2321
};
24-
sort(0, n);
25-
return nums[k - 1];
22+
return quickSort(0, n - 1);
2623
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int findKthLargest(vector<int>& nums, int k) {
4+
priority_queue<int, vector<int>, greater<int>> minQ;
5+
for (int x : nums) {
6+
minQ.push(x);
7+
if (minQ.size() > k) {
8+
minQ.pop();
9+
}
10+
}
11+
return minQ.top();
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func findKthLargest(nums []int, k int) int {
2+
minQ := hp{}
3+
for _, x := range nums {
4+
heap.Push(&minQ, x)
5+
if minQ.Len() > k {
6+
heap.Pop(&minQ)
7+
}
8+
}
9+
return minQ.IntSlice[0]
10+
}
11+
12+
type hp struct{ sort.IntSlice }
13+
14+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
15+
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
16+
func (h *hp) Pop() any {
17+
a := h.IntSlice
18+
v := a[len(a)-1]
19+
h.IntSlice = a[:len(a)-1]
20+
return v
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int findKthLargest(int[] nums, int k) {
3+
PriorityQueue<Integer> minQ = new PriorityQueue<>();
4+
for (int x : nums) {
5+
minQ.offer(x);
6+
if (minQ.size() > k) {
7+
minQ.poll();
8+
}
9+
}
10+
return minQ.peek();
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def findKthLargest(self, nums: List[int], k: int) -> int:
3+
return nlargest(k, nums)[-1]
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
1-
use rand::Rng;
1+
use std::collections::BinaryHeap;
22

33
impl Solution {
4-
pub fn find_kth_largest(mut nums: Vec<i32>, k: i32) -> i32 {
5-
let k = k as usize;
6-
let n = nums.len();
7-
let mut l = 0;
8-
let mut r = n;
9-
while l <= k - 1 && l < r {
10-
nums.swap(l, rand::thread_rng().gen_range(l, r));
11-
let num = nums[l];
12-
let mut mark = l;
13-
for i in l..r {
14-
if nums[i] > num {
15-
mark += 1;
16-
nums.swap(i, mark);
17-
}
18-
}
19-
nums.swap(l, mark);
20-
if mark + 1 <= k {
21-
l = mark + 1;
22-
} else {
23-
r = mark;
4+
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
5+
let mut minQ = BinaryHeap::new();
6+
for &x in nums.iter() {
7+
minQ.push(-x);
8+
if minQ.len() > k as usize {
9+
minQ.pop();
2410
}
2511
}
26-
nums[k - 1]
12+
-minQ.peek().unwrap()
2713
}
2814
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function findKthLargest(nums: number[], k: number): number {
2+
const minQ = new MinPriorityQueue();
3+
for (const x of nums) {
4+
minQ.enqueue(x);
5+
if (minQ.size() > k) {
6+
minQ.dequeue();
7+
}
8+
}
9+
return minQ.front().element;
10+
}

0 commit comments

Comments
 (0)