We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ff21319 commit 5139d8aCopy full SHA for 5139d8a
solution/0200-0299/0215.Kth Largest Element in an Array/Solution3.rs
@@ -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