Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Solution: 'K' Closest Points to the Origin

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

Why not

def findClosestPoints(self, points, k): result = [] min_heap = [] for point in points: heappush(min_heap, (point.distance_from_origin(), point)) for i in range(k): tup = heappop(min_heap) result.append([tup[1].x, tup[1].y]) return result
Show 1 reply
A

Anonymous

· 3 years ago

  1. Generally def _ _ lt _ _(self, other) is used for "Less than". However, in definition it has used ">" sign (Which is used for Greater than)
  2. How will maxHeap will work? since we are pushing object of class Point. How will heap sort it?
Show 1 reply
A

Anonymous

· 4 years ago

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

Show 3 replies

Reading Progress

0%


Vote for new content