Skip to content

Commit

Permalink
Problem to find nearby cars
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush1oo8 committed Mar 9, 2024
1 parent 6ed252b commit e6271f2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Heap/NearbyCars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Heap;
import java.util.*;
public class NearbyCars {
static class Point implements Comparable<Point>{
int x;
int y;
int disq;
int idx;
public Point(int x, int y, int disq,int idx){
this.x=x;
this.y=y;
this.disq=disq;
this.idx=idx;
}
@Override
public int compareTo(Point p2 ){
return this.disq-p2.disq;
}
}
public static void main(String[] args) {
int pts[][]={{3,3},{5,-1},{-2,4}};
int k=2;

PriorityQueue<Point> p=new PriorityQueue<>();
for(int i=0;i<pts.length;i++){
int disq=pts[i][0]*pts[i][0]+pts[i][1]*pts[i][1];
p.add(new Point(pts[i][0],pts[i][1],disq,i));

}
for(int i=0;i<k;i++){
System.out.println("C"+p.remove().idx);
}
}
}

0 comments on commit e6271f2

Please sign in to comment.