340. Dot Product of Two Sparse Vectors

MediumHash Table

Given two sparse vectors nums1 and nums2 (arrays that are mostly zeros), compute their dot product. The dot product is the sum of nums1[i] * nums2[i] over all indices i. A sparse representation stores only the non-zero entries so the product skips the many zeros.

Examples

Input: [1,0,0,2,3] [0,3,0,4,0]

Output: 8

Explanation: Only index 3 has both non-zero: 2*4 = 8.

Constraints

  • n == nums1.length == nums2.length
  • 1 <= n <= 10^5
  • 0 <= nums1[i], nums2[i] <= 100
Loading...

Run checks all cases above. Submit evaluates all test cases.