Skip to content

Commit 4ba94a8

Browse files
authored
Merge pull request #22 from Arnab7456/main
Integer Overflow in Max Heap Comparator for K Closest Points to Origin
2 parents a404f08 + 554f53e commit 4ba94a8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Diff for: patterns/java/TopKElements.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ private int getDistance(int[] point) {
126126

127127
public int[][] kClosestPointsToOriginMaxHeapApproach(int[][] points, int k) {
128128
// Max heap with custom comparator to compare by distance
129-
PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> getDistance(b) - getDistance(a));
129+
PriorityQueue<int[]> maxHeap = new PriorityQueue<>(
130+
(a, b) -> Integer.compare(getDistance(b), getDistance(a))
131+
);
130132

131133
// Iterate through all points
132134
for (int[] point : points) {
@@ -146,4 +148,4 @@ public int[][] kClosestPointsToOriginMaxHeapApproach(int[][] points, int k) {
146148

147149
return result;
148150
}
149-
}
151+
}

0 commit comments

Comments
 (0)