すべてのチャレンジ
簡単 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 つずつ開いてみましょう。

ヒント 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レビューも見ることができます。 サインインして課題を解くと議論に参加できます。