Skip to content

Commit 8a1d3d4

Browse files
authored
Merge pull request neetcode-gh#2217 from Jay-0331/rust/0973-k-closest-points-to-origin
Create: 0973-k-closest-points-to-origin.rs
2 parents e676cf8 + e425460 commit 8a1d3d4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::{cmp::Reverse, collections::BinaryHeap};
2+
3+
impl Solution {
4+
pub fn k_closest(points: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
5+
let mut pts = BinaryHeap::new();
6+
for point in points {
7+
let dist = (point[0].pow(2) + point[1].pow(2));
8+
pts.push(Reverse((dist, point[0], point[1])));
9+
}
10+
11+
let mut res: Vec<Vec<i32>> = vec![];
12+
for i in 0..k {
13+
match pts.pop() {
14+
Some(Reverse((dist, x, y))) => res.push(vec![x,y]),
15+
None => {}
16+
}
17+
}
18+
res
19+
}
20+
}

0 commit comments

Comments
 (0)