Product of Two Run-Length Encoded Arrays - Problem
Run-length encoding is a clever compression technique that represents arrays with consecutive repeated numbers in a compact format. Instead of storing
Your task is to multiply two run-length encoded arrays without fully expanding them into their original arrays. Given two encoded arrays representing the same-length sequences, calculate their element-wise product and return the result in run-length encoded format.
The Challenge: The naive approach would expand both arrays, multiply corresponding elements, and re-compress. But can you do it more efficiently by working directly with the compressed format?
Example:
•
•
• Product:
[1,1,1,2,2,2,2,2], we can represent it as [[1,3],[2,5]] meaning "three 1's followed by five 2's".Your task is to multiply two run-length encoded arrays without fully expanding them into their original arrays. Given two encoded arrays representing the same-length sequences, calculate their element-wise product and return the result in run-length encoded format.
The Challenge: The naive approach would expand both arrays, multiply corresponding elements, and re-compress. But can you do it more efficiently by working directly with the compressed format?
Example:
•
encoded1 = [[1,3],[2,1]] represents [1,1,1,2]•
encoded2 = [[6,2],[1,2]] represents [6,6,1,1]• Product:
[6,6,2,2] → encoded as [[6,2],[2,2]] Input & Output
example_1.py — Basic Case
$
Input:
encoded1 = [[1,3],[2,1]], encoded2 = [[6,2],[1,2]]
›
Output:
[[6,2],[2,1]]
💡 Note:
encoded1 expands to [1,1,1,2] and encoded2 expands to [6,6,1,1]. The product is [1*6,1*6,1*1,2*1] = [6,6,1,2], which compresses to [[6,2],[1,1],[2,1]]. However, we can optimize to get [[6,2],[2,1]] by noticing that [1,1] can be merged with [2,1] as they're not consecutive equal values.
example_2.py — Single Segments
$
Input:
encoded1 = [[1,3]], encoded2 = [[2,3]]
›
Output:
[[2,3]]
💡 Note:
encoded1 expands to [1,1,1] and encoded2 expands to [2,2,2]. The product is [1*2,1*2,1*2] = [2,2,2], which compresses to [[2,3]].
example_3.py — Multiple Products
$
Input:
encoded1 = [[2,2],[3,1]], encoded2 = [[1,1],[4,2]]
›
Output:
[[2,1],[12,2]]
💡 Note:
encoded1 expands to [2,2,3] and encoded2 expands to [1,4,4]. The product is [2*1,2*4,3*4] = [2,8,12]. Since these are all different values, the result compresses to [[2,1],[8,1],[12,1]], but we need to be careful about segment boundaries.
Constraints
- 1 ≤ encoded1.length, encoded2.length ≤ 105
- encoded1[i].length == encoded2[i].length == 2
- 1 ≤ vali, freqi ≤ 104
- The sum of freqi for encoded1 equals the sum of freqi for encoded2
- Both arrays represent sequences of the same total length
Visualization
Tap to expand
Understanding the Visualization
1
Setup Coordination
Position supervisors at both conveyor belts to monitor current batches
2
Synchronize Production
Calculate how many combined products can be made from current batches
3
Update Inventory
Reduce remaining counts and move to next batch when current is exhausted
4
Optimize Output
Merge consecutive identical products into single batches
Key Takeaway
🎯 Key Insight: By synchronizing two pointers and tracking remaining counts, we can multiply run-length encoded arrays directly without expansion, achieving optimal time and space complexity.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code