forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2217 from Jay-0331/rust/0973-k-closest…
…-points-to-origin Create: 0973-k-closest-points-to-origin.rs
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::{cmp::Reverse, collections::BinaryHeap}; | ||
|
||
impl Solution { | ||
pub fn k_closest(points: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> { | ||
let mut pts = BinaryHeap::new(); | ||
for point in points { | ||
let dist = (point[0].pow(2) + point[1].pow(2)); | ||
pts.push(Reverse((dist, point[0], point[1]))); | ||
} | ||
|
||
let mut res: Vec<Vec<i32>> = vec![]; | ||
for i in 0..k { | ||
match pts.pop() { | ||
Some(Reverse((dist, x, y))) => res.push(vec![x,y]), | ||
None => {} | ||
} | ||
} | ||
res | ||
} | ||
} |