Product of Two Run-Length Encoded Arrays — LeetCode 1868 Python Solution

MediumLeetCode PremiumArrayTwo Pointers
Problem
#1868
Reading time
4 min

The 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] = [vali, freqi] describes the ith segment of repeated numbers in nums where vali is the value that is repeated freqi times.

Example

Input
encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]]
Output
[[6,6]]
Explanation
encoded1 expands to [1,1,1,2,2,2] and encoded2 expands to [6,6,6,3,3,3].

Python solution

Python
class Solution:
    def findRLEArray(
        self, encoded1: List[List[int]], encoded2: List[List[int]]
    ) -> List[List[int]]:
        ans = []
        j = 0
        for vi, fi in encoded1:
            while fi:
                f = min(fi, encoded2[j][1])
                v = vi * encoded2[j][0]
                if ans and ans[-1][0] == v:
                    ans[-1][1] += f
                else:
                    ans.append([v, f])
                fi -= f
                encoded2[j][1] -= f
                if encoded2[j][1] == 0:
                    j += 1
        return ans

Complexity

MeasureComplexity
TimeO(n) (after optional sort O(n log n))
SpaceO(1) auxiliary

Pattern: Two Pointers

Use the order already in the input to discard half the search space at every step. LeetCode 1868. Product of Two Run-Length Encoded Arrays is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Two Pointers.

The two pointers guide has the Python template for the pattern and the 201 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1868. Product of Two Run-Length Encoded Arrays?
LeetCode 1868. Product of Two Run-Length Encoded Arrays is rated Medium on LeetCode.
What is the time complexity of LeetCode 1868. Product of Two Run-Length Encoded Arrays?
The Python solution on this page runs in O(n) (after optional sort O(n log n)).
What is the space complexity of LeetCode 1868. Product of Two Run-Length Encoded Arrays?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1868. Product of Two Run-Length Encoded Arrays cover?
LeetCode 1868. Product of Two Run-Length Encoded Arrays is tagged Array and Two Pointers on LeetCode.
Is LeetCode 1868. Product of Two Run-Length Encoded Arrays a premium problem?
Yes. LeetCode 1868. Product of Two Run-Length Encoded Arrays is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview