题目说明
编写 function solution(documents, query, limit)。输入输出均为 JSON 值,不要修改输入数组或对象。不支持外部库、网络访问及异步函数。 documents:{id:string,vector:integer[]}[], query:integer[], limit:nonnegative integer. Vectors share dimension 1–32 with values -100…100. Cosine=dot/(product of magnitudes); use zero if either vector is zero. score=Math.round(cosine*1_000_000). Sort by rounded score descending, preserving input order on ties. Return top limit {id,score}; limit may exceed document count. 输入范围:每个输入数组最多50,000项,每个字符串最多1,000个字符。数字必须有限,整数及整数累计结果在JavaScript安全整数范围内。其他类型和有效性遵循上述规则。
输入输出示例
示例 1
函数参数
[
[
{
"id": "a",
"vector": [
1,
0
]
},
{
"id": "b",
"vector": [
0,
1
]
}
],
[
1,
0
],
2
]返回值
[
{
"id": "a",
"score": 1000000
},
{
"id": "b",
"score": 0
}
]示例 2
函数参数
[
[
{
"id": "a",
"vector": [
1,
1
]
},
{
"id": "b",
"vector": [
2,
2
]
}
],
[
1,
1
],
1
]返回值
[
{
"id": "a",
"score": 1000000
}
]示例 3
函数参数
[
[
{
"id": "a",
"vector": [
1,
0
]
},
{
"id": "b",
"vector": [
0,
1
]
},
{
"id": "c",
"vector": [
1,
0
]
}
],
[
1,
0
],
2
]返回值
[
{
"id": "a",
"score": 1000000
},
{
"id": "c",
"score": 1000000
}
]