Grokking the Coding Interview: Patterns for Coding Questions
0% completed
'K' Closest Points to the Origin (easy)
.....
.....
.....
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
- Generally def _ _ lt _ _(self, other) is used for "Less than". However, in definition it has used ">" sign (Which is used for Greater than)
- 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%