모든 챌린지
쉬움 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 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.