Skip to content

Commit

Permalink
Create 0215-kth-largest-element-in-an-array.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
felivalencia3 authored Aug 4, 2023
1 parent 56ad2c6 commit 2586f93
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rust/0215-kth-largest-element-in-an-array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
https://leetcode.com/problems/kth-largest-element-in-an-array/submissions/1012381844/

use std::collections::BinaryHeap;
impl Solution {
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
let mut heap = BinaryHeap::new();
for &n in nums.iter() {
if heap.len() < k as usize {
heap.push(-n);
continue;
} else if -heap.peek().unwrap() < n {
heap.pop();
heap.push(-n);
}
}
-heap.pop().unwrap()
}
}

0 comments on commit 2586f93

Please sign in to comment.