LeetCode 1868, Medium. Topics: Array, Two Pointers. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 24 parametrized pytest cases, and a playground notebook:
Problem
Run-length encoding is a compression algorithm that allows for an integer array nums with many segments of consecutive repeated numbers to be represented by a (generally smaller) 2D array encoded. Each encoded[i] = [val_i, freq_i] describes the i-th segment of repeated numbers in nums where val_i is the value that is repeated freq_i times.
- For example,
nums = [1,1,1,2,2,2,2,2] is represented by the run-length encoded array encoded = [[1,3],[2,5]]. Another way to read this is “three 1’s followed by five 2’s”.
The product of two run-length encoded arrays encoded1 and encoded2 can be calculated using the following steps:
- Expand both
encoded1 and encoded2 into the full arrays nums1 and nums2 respectively.
- Create a new array
prodNums of length nums1.length and set prodNums[i] = nums1[i] * nums2[i].
- Compress
prodNums into a run-length encoded array and return it.
You are given two run-length encoded arrays encoded1 and encoded2 representing full arrays nums1 and nums2 respectively. Both nums1 and nums2 have the same length. Each encoded1[i] = [val_i, freq_i] describes the i-th segment of nums1, and each encoded2[j] = [val_j, freq_j] describes the j-th segment of nums2.
Return the product of encoded1 and encoded2.
Note: Compression should be done such that the run-length encoded array has the minimum possible length.
Examples
Constraints
- 1 <= encoded1.length, encoded2.length <= 10^5
- encoded1[i].length == 2
- encoded2[j].length == 2
- 1 <= val_i, freq_i <= 10^4 for each encoded1[i]
- 1 <= val_j, freq_j <= 10^4 for each encoded2[j]
- The full arrays that encoded1 and encoded2 represent are the same length.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026