📘
Dot Product of Two Sparse Vectors
Given two sparse vectors represented as arrays, compute their dot product efficiently.
A sparse vector has mostly zero elements. Store only non-zero elements for efficiency.
Example:
Input:
1,0,0,2,3 0,3,0,4,0
Output:
8
Reasoning:
- The input represents two sparse vectors: [1,0,0,2,3] and [0,3,0,4,0]
- To compute the dot product, we multiply corresponding elements: 1⋅0+0⋅3+0⋅0+2⋅4+3⋅0
- This simplifies to: 0+0+0+8+0=8
- The final output is the result of this computation: 8
Constraints:
- 1 <= len(nums1) == len(nums2) <= 10^5
- 0 <= nums[i] <= 100
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.