FunctionRacerProblemsRace

Vote of the Nearest

HardNumPyPython

Each row of train is a point with a label in labels (0 to 4). For each row of queries, find the k rows of train nearest to it (straight-line distance; on a tie, the lower row first) and return the label most of them have. If labels tie, return the smallest. No loops: for and while are not allowed.

Example 1

train [[0,0],[1,0],[0,1],[9,9],[8,9]]
labels [0,0,1,1,1]
queries [[0,0],[9,8]]
k 3
returns [0,1]

The three points nearest (0, 0) carry labels 0, 0 and 1, so 0 wins. Nearest (9, 8) are the two points with label 1 and then, far away, one of the others.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More NumPy problems