Vote of the Nearest
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
1 <= k <= len(train) <= 401 <= len(queries) <= 20- Points have 1 to 3 columns of whole numbers from -15 to 15
0 <= labels[i] <= 4
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More NumPy problems
- Above Average CountBeginner
- Average TemperatureBeginner
- Black and WhiteBeginner
- Celsius to FahrenheitBeginner
- Clip ScoresBeginner
- CorrelationBeginner
- Count DistinctBeginner
- Count PositivesBeginner