Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Programs/K_Closest_Points_to _Origin-Heap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Minimum Heap Example:

K Closest Points to Origin
- Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k,
return the k closest points to the origin (0, 0).
The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2).
Example
Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]

Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].

*/



class Solution {
public int[][] kClosest(int[][] points, int k) {
PriorityQueue<Closest> minHeap = new PriorityQueue<>((a,b)->
{
return a.getSum()-b.getSum();}
);

for(int[] point : points){
int x= point[0];
int y = point[1];
Closest closestObj = new Closest(x,y);
minHeap.add(closestObj);
}

int[][] result = new int[k][2];
int index = 0;
while(index < k){
Closest closestObj = minHeap.poll();
result[index][0]=closestObj.x;
result[index][1]=closestObj.y;
index++;
}
return result ;
}
}
public class Closest{
public int x;
public int y;
Closest(int x, int y){
this.x=x;
this.y=y;
}
public int getSum(){
return (this.x*this.x)+(this.y*this.y);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ It is very easy to contribute, you may follow these steps -
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
102. [K closest Point to Origin by Heap](https://github.com/PrajaktaSathe/Java/pull/169/commits/4733570db80491cf7205836a16bfb756e91522bd) - K closest Point to Origin by Heap

# Contributors -
## A big thank you to all our contributors!!!
Expand Down