Rank Vectors by Dot-Product Similarity
Problem statement
For this exercise, measure similarity with the dot product. Given an integer query vector query and candidate vectors vectors of the same dimension, the score of candidate i is the sum of query[j] * vectors[i][j] over every coordinate.
Return every candidate’s zero-based index in decreasing score order. Break equal scores by increasing index. Do not normalize vectors or return the score values. An empty candidate list returns an empty result.
Function
rankVectors(query: int[], vectors: int[][]) → int[]Examples
Example 1
query = [2,-1]vectors = [[1,0],[0,3],[2,2],[-1,-2]]return = [0,2,3,1]The scores in input order are 2, -3, 2 and 0. Indices 0 and 2 tie, so index 0 comes first. The result is [0,2,3,1].
Example 2
query = [0,0]vectors = [[9,-4],[0,0],[-2,8]]return = [0,1,2]A zero query gives every candidate score 0. The tie rule therefore returns the original index order [0,1,2].
Constraints
- 1 <= query.length <= 200
- 0 <= vectors.length <= 2000
- 1 <= vectors[i].length <= 200
- Every candidate has the same length as query.
- -1000 <= query[j] <= 1000
- -1000 <= vectors[i][j] <= 1000