Skip to content

Commit 2586f93

Browse files
Create 0215-kth-largest-element-in-an-array.rs
Leetcode Submission: https://leetcode.com/problems/kth-largest-element-in-an-array/submissions/1012381844/
1 parent 56ad2c6 commit 2586f93

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: rust/0215-kth-largest-element-in-an-array.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
https://leetcode.com/problems/kth-largest-element-in-an-array/submissions/1012381844/
2+
3+
use std::collections::BinaryHeap;
4+
impl Solution {
5+
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
6+
let mut heap = BinaryHeap::new();
7+
for &n in nums.iter() {
8+
if heap.len() < k as usize {
9+
heap.push(-n);
10+
continue;
11+
} else if -heap.peek().unwrap() < n {
12+
heap.pop();
13+
heap.push(-n);
14+
}
15+
}
16+
-heap.pop().unwrap()
17+
}
18+
}

0 commit comments

Comments
 (0)