Skip to content

Commit 5139d8a

Browse files
authored
Create Solution3.rs
1 parent ff21319 commit 5139d8a

File tree

1 file changed

+27
-0
lines changed
  • solution/0200-0299/0215.Kth Largest Element in an Array

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
5+
let mut cnt = HashMap::new();
6+
let mut m = i32::MIN;
7+
8+
for &x in &nums {
9+
*cnt.entry(x).or_insert(0) += 1;
10+
if x > m {
11+
m = x;
12+
}
13+
}
14+
15+
let mut k = k;
16+
for i in (i32::MIN..=m).rev() {
17+
if let Some(&count) = cnt.get(&i) {
18+
k -= count;
19+
if k <= 0 {
20+
return i;
21+
}
22+
}
23+
}
24+
25+
unreachable!();
26+
}
27+
}

0 commit comments

Comments
 (0)