Practice: K Closest Points to Origin

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

Recognition reminder: you want the k points nearest the origin, in any order. This is the size-k heap again, but with a twist: the thing you order by (distance to the origin) is computed, and the thing you keep (the point itself) is the payload. That split is exactly what tuples-as-heap-entries are for.

Before you start (the five-beat rhythm)

  1. Your Pattern Card for this week is already written.
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

Sorting all points by distance is O(n log n) and works, but the problem only asks for k of them. To carry a heap of size k, decide: since you want the k closest (the k smallest distances), which survivor do you need to see and evict when a closer point arrives, and does that make this a min-heap or a max-heap of size k? Two more things to settle before you code: you can compare squared distances and skip the square root entirely (it does not change the ordering), and your heap entries will be tuples, so think about what goes first (the key you sort on) and whether the rest of the tuple can always be compared if two distances tie.

The function you implement

Write a function k_closest(points: list[list[int]], k: int) -> list[list[int]] in a file named solution.py in this folder (your own work repo, not the course repo). Each point is a two-element list [x, y]. Return the k points closest to the origin (0, 0), as a list of [x, y] points, in any order. As the LeetCode constraints guarantee, 1 <= k <= len(points), and the answer is unique except for the order, so you do not have to break ties between equidistant points in any particular way.

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?