Gamified challenge · Max-heap of size k
You are at (0, 0). taxis[i] = [x, y] is where taxi i is; no two taxis share a spot. A taxi's distance is the straight-line distance √(x² + y²), so comparing x² + y² is enough.
Write kClosest(taxis, k) (Python k_closest(taxis, k)) returning the k taxis nearest to you as [x, y] pairs, in any order. k is between 0 and the number of taxis. If several taxis tie at the cut-off distance, any of them may be returned.
Example: [[3, 3], [5, -1], [-2, 4]] with k = 2 → `[[3, 3], [-2, 4]]` (squared distances 18 and 20; the third is 26).
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google