Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2217 from Jay-0331/rust/0973-k-closest…
Browse files Browse the repository at this point in the history
…-points-to-origin

Create: 0973-k-closest-points-to-origin.rs
  • Loading branch information
tahsintunan committed Feb 10, 2023
2 parents e676cf8 + e425460 commit 8a1d3d4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rust/0973-k-closest-points-to-origin.rs
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
}
}

0 comments on commit 8a1d3d4

Please sign in to comment.