ง่าย 8 ข้อที่แก้แล้ว อัตราผ่าน 44.4%

Dot Product of Two Vectors

Multiply two vectors element by element and add the results: the core operation of every neuron.

สิ่งที่คุณจะได้เรียนรู้

  • Multiply vectors element by element and sum
  • Understand the core operation of a neuron
  • Work with number arrays

โจทย์

ดูฉบับแปล

Every neuron starts with the same calculation: multiply each input by its weight and add everything up. That is the dot product of two vectors:

a · b = a₁×b₁ + a₂×b₂ + … + aₙ×bₙ

Implement dot_product(a, b).

Input

a, b: Two lists of numbers with the same length.

Output

Return the dot product as a decimal number.

เงื่อนไข

1 ≤ length of a = length of b ≤ 1,000

-1,000 ≤ each value ≤ 1,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]

ผลลัพธ์ที่คาดหวัง:
expected = 32.0
อินพุต:
std::vector<double> a = {1.0, 2.0, 3.0};
std::vector<double> b = {4.0, 5.0, 6.0};

ผลลัพธ์ที่คาดหวัง:
double expected = 32.0;
อินพุต:
double[] a = new double[] {1.0, 2.0, 3.0};
double[] b = new double[] {4.0, 5.0, 6.0};

ผลลัพธ์ที่คาดหวัง:
double expected = 32.0;

คำอธิบาย

1×4 + 2×5 + 3×6 = 4 + 10 + 18 = 32.

ตัวอย่างที่ 2
อินพุต:
a = [0.5, -1.5]
b = [2.0, 4.0]

ผลลัพธ์ที่คาดหวัง:
expected = -5.0
อินพุต:
std::vector<double> a = {0.5, -1.5};
std::vector<double> b = {2.0, 4.0};

ผลลัพธ์ที่คาดหวัง:
double expected = -5.0;
อินพุต:
double[] a = new double[] {0.5, -1.5};
double[] b = new double[] {2.0, 4.0};

ผลลัพธ์ที่คาดหวัง:
double expected = -5.0;

คำอธิบาย

0.5×2 + (-1.5)×4 = 1 - 6 = -5.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Multiply matching positions from the two lists and combine the results into one total.

คำใบ้ 2

A single pass is enough because each pair contributes independently to the final value.

คำใบ้ 3

Make sure the lists have the same length, and keep the accumulator in a numeric type that preserves decimals if needed.

ความซับซ้อนที่คาดหวัง

O(n) time, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 ННикита Python 792 ms - 5/5 ดูโค้ด
2 ゼゼロダニ Python 812 ms - 4/5 ดูโค้ด
3 HJhettory Jurash Python 840 ms - 5/5 ดูโค้ด
4 KKKaneki Ken Python 862 ms - 4/5 ดูโค้ด
5 RARichard Adjei Python 1,252 ms - 4/5 ดูโค้ด
6 FSFilippo Sanguineti C++ 3,126 ms - 4/5 ดูโค้ด
7 PTphúc thịnh phạm C++ 3,240 ms - 3/5 ดูโค้ด
8 ·𝓕·.★·.·⍣·.·★ 𝓕𝓪𝓫𝓲𝓸𝓵𝓪 𝓔𝓵𝓮𝓷𝓪 ★·.·⍣·.·★.· C++ 7,069 ms - 4/5 ดูโค้ด

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

สปอยล์: มีคำตอบเต็ม 8 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย