-
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.
- Loading branch information
1 parent
6ed252b
commit e6271f2
Showing
1 changed file
with
34 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,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); | ||
} | ||
} | ||
} |