K-Means Assignment Step
Description
Given a list of points and a list of cluster centroids (both lists of equal-length coordinate vectors), perform one k-means assignment step: return an array where each entry is the index of the centroid closest to the corresponding point by Euclidean distance. Break ties toward the lower centroid index.
Examples
[[1,1],[2,2],[8,8],[9,9]], [[0,0],[10,10]][0,0,1,1]Each point is labeled with the index of the closest centroid measured by Euclidean distance, breaking any tie toward the lower index.
[[1,2],[3,4],[10,10]], [[2,3],[9,9]][0,0,1]Each point is labeled with the index of the closest centroid measured by Euclidean distance, breaking any tie toward the lower index.
[[0,0],[5,5]], [[0,0],[5,5]][0,1]Each point is labeled with the index of the closest centroid measured by Euclidean distance, breaking any tie toward the lower index.
Constraints
- •
1 ≤ points, centroids ≤ 10³ - •
All vectors share the same dimension
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.