Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created April 4, 2021 20:04
Show Gist options
  • Select an option

  • Save mvallebr/6f92d3ae10a72287688a024fc4c2882e to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/6f92d3ae10a72287688a024fc4c2882e to your computer and use it in GitHub Desktop.
import heapq
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
points_gen = ((-(x*x+y*y), (x, y)) for x, y in points)
result = []
for point in points_gen:
heapq.heappush(result, point)
if len(result) > k:
heapq.heappop(result)
return [point for _, point in result]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment