1868. Product of Two Run Length Encoded Arrays (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution(object):
    def findRLEArray(self, encoded1, encoded2):
        """
        :type encoded1: List[List[int]]
        :type encoded2: List[List[int]]
        :rtype: List[List[int]]
        """
        i = j = -1
        n1, n2 = len(encoded1), len(encoded2)
        result = []
        a = b = 0
        while i < n1:
            if not a:
                i += 1
                if i == n1:
                    break
                a = encoded1[i][1]
            if not b:
                j += 1
                if j == n2:
                    break
                b = encoded2[j][1]
            x = encoded1[i][0] * encoded2[j][0]
            m = min(a, b)
            if result and result[-1][0] == x:
                result[-1][1] += m
            else:
                result.append([x, m])
            a -= m
            b -= m
        return result