A delivery map shows the k stores closest to the warehouse, which sits at [0, 0]. stores holds each store's position as [x, y]. Distance is the usual straight-line distance.
Write closestStores(stores, k) that returns the k closest positions, in any order. The tests guarantee the answer is unique (no tie at the cutoff).
Input: stores = [[1,3],[-2,2]], k = 1 Output: [[-2,2]]
[-2, 2] is about 2.83 away; [1, 3] is about 3.16 away.
Input: stores = [[3,3],[5,-1],[-2,4]], k = 2 Output: [[3,3],[-2,4]]
Squared distances are 18, 26, and 20. The two smallest are 18 and 20.
1 <= k <= stores.length <= 10^5-10^4 <= x, y <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #216 Closest Stores to the Warehouse. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.