คำอธิบายโจทย์
เขียน 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
}
]