Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 554f53e

Browse files
Integer Overflow in Max Heap Comparator for K Closest Points to Origin
The current implementation can potentially cause overflow when the distances are large. issue solve Id: #21
1 parent 3f41a4a commit 554f53e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

‎patterns/java/TopKElements.java‎

Lines changed: 4 additions & 2 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /