K-Means Assignment Step

MediumMachine LearningMathArray

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

Input:[[1,1],[2,2],[8,8],[9,9]], [[0,0],[10,10]]
Output:[0,0,1,1]
Explanation:

Each point is labeled with the index of the closest centroid measured by Euclidean distance, breaking any tie toward the lower index.

Input:[[1,2],[3,4],[10,10]], [[2,3],[9,9]]
Output:[0,0,1]
Explanation:

Each point is labeled with the index of the closest centroid measured by Euclidean distance, breaking any tie toward the lower index.

Input:[[0,0],[5,5]], [[0,0],[5,5]]
Output:[0,1]
Explanation:

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.