Dot Product of Two Run-Length Encoded Vectors
Arrays & HashingAuto-graded · 18 test casesLast updated
Compute the dot product of two run-length encoded integer vectors by walking both run lists together, without expanding them into full arrays.
Dot Product of Run-Length Encoded Vectors
A long integer vector often contains stretches of equal values. To save memory, such a vector can be stored as a list of runs, where each run is a pair [value, count] meaning that value appears count times in a row. For example, [[1, 2], [3, 1]] stands for the vector [1, 1, 3].
You are given two vectors in this run-length form, first and second. Return their dot product: the sum of products of the elements at matching positions of the two expanded vectors.
Do not build the expanded vectors. Your solution should depend on the number of runs, not on the expanded length.
Details
- The run boundaries of the two vectors usually do not line up, so a run in one vector may span several runs in the other.
- Runs are processed in the order given.
- If one vector is exhausted before the other (different expanded lengths), only the positions covered by both vectors contribute. Positions beyond the shorter vector add nothing.
- If either list is empty, the result is
0. - A run with
countequal to0contributes nothing. - Values may be zero or negative.
Function
Implement run_length_dot(first, second) which returns the dot product as an integer.
Constraints
- Each expanded vector can have up to 10^6 elements, while each list has far fewer runs (around 10^3).
- Every run is a two-element list
[value, count]with integervalueand non-negative integercount. - Results fit in Python's arbitrary-precision integers.
Examples
Example 1
Input: run_length_dot([[1, 2], [3, 1]], [[2, 1], [1, 2]])
Output: 6
Example 2
Input: run_length_dot([[5, 10]], [[2, 5], [3, 5]])
Output: 125
Hints
Hint 1
Expanding both vectors costs time and memory proportional to the expanded length. Think about what the product looks like over a stretch where both vectors are constant.
Hint 2
Keep one pointer per vector and a counter of how many elements remain in the current run of each. The next aligned stretch has length equal to the smaller of the two counters.
Hint 3
After consuming a stretch, subtract its length from both counters. Whichever counter reaches zero moves its pointer to the next run. Use two independent checks so that both can advance at once.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Never expand the vectors. Over any stretch where both vectors are constant, with values a and b over L positions, the contribution to the dot product is simply a * b * L.
Use two pointers, one per run list, and track how many elements remain unused in the current run of each vector:
- Take
step = min(left1, left2). This is the length of the stretch during which both current runs stay constant. - Add
a * b * stepto the total. - Subtract
stepfrom both counters. - If a counter reached zero, move that vector's pointer to the next run and load its count. The two checks are independent (not
if/elif), so when both runs end together both pointers advance.
Stop when either list runs out of runs.
Why it is correct
At the start of each iteration, both vectors have consumed the same number of logical positions, and every earlier position has been added exactly once. The next step positions lie inside the current run of both vectors, so each product there equals a * b, and the sum is a * b * step. Both sides advance by step, so they stay aligned. Each iteration finishes at least one run, so the loop terminates. If the lengths differ, the loop ends when the shorter vector is exhausted, which matches the rule that only commonly covered positions count. Empty lists skip the loop and return 0, and zero-count runs give step = 0, contribute nothing, and are skipped.
Complexity
- Time:
O(M + K)forMandKruns, independent of the expanded length. - Space:
O(1)extra.
Reference solution
from typing import List
def run_length_dot(first: List[List[int]], second: List[List[int]]) -> int:
i = j = 0
left1 = first[0][1] if first else 0
left2 = second[0][1] if second else 0
total = 0
while i < len(first) and j < len(second):
step = min(left1, left2)
total += first[i][0] * second[j][0] * step
left1 -= step
left2 -= step
if left1 == 0:
i += 1
if i < len(first):
left1 = first[i][1]
if left2 == 0:
j += 1
if j < len(second):
left2 = second[j][1]
return total