Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
How does just pushing ' points[i]' let the heap know how to maintain the furthes...

Derek Yu

Jul 21, 2022

How does just pushing ' points[i]' let the heap know how to maintain the furthest distance in the heap?

0

0

Comments
Comments
Design Gurus
Design Gurus3 years ago

We used a max heap. This heap uses the distance from the origin as the comparison operator. From code:

def lt(self, other): return self.distance_from_origin() > other.distance_from_origin()

This means the heap keeps the farthest point at the top. We compare other...

V
Victor An3 years ago

https://leetcode.com/problems/k-closest-points-to-origin

class Pair {
// getter...
int index;
long distanceToOrigin;

Pair(int idx, int[] point) {
this.index = idx;
this.distanceToOrigin = getDistanceTo...
K
k 2 years ago

.

On this page