Product of Two Run-Length Encoded Arrays

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.

  • 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:

  1. Expand both encoded1 and encoded2 into the full arrays nums1 and nums2 respectively.
  2. Create a new array prodNums of length nums1.length and set prodNums[i] = nums1[i] * nums2[i].
  3. 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] = [vali, freqi] describes the ith segment of nums1, and each encoded2[j] = [valj, freqj] describes the jth 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.

Example 1:

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].
prodNums = [6,6,6,6,6,6], which is compressed into the run-length encoded array [[6,6]].

思路:双指针,value是前面的相乘,freq是取两个pair[1]的最小,然后pair[1] -= freq. index 相应的前进,然后要跟res的最后一个encoding list要比较,如果value相等,那么freq加进去,否则加入一个新的区间;

class Solution {
    public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) {
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        int index1 = 0;
        int index2 = 0;
        while(index1 < encoded1.length && index2 < encoded2.length) {
            int productVal = encoded1[index1][0] * encoded2[index2][0];
            int fre = Math.min(encoded1[index1][1], encoded2[index2][1]);
            
            encoded1[index1][1] -= fre;
            encoded2[index2][1] -= fre;
            if(encoded1[index1][1] == 0) {
                index1++;
            }
            if(encoded2[index2][1] == 0) {
                index2++;
            }
            
            if(lists.isEmpty()) {
                List<Integer> list = new ArrayList<>();
                list.add(productVal);
                list.add(fre);
                lists.add(list);
            } else {
                List<Integer> lastList = lists.get(lists.size() - 1);
                if(lastList.get(0) != productVal) {
                    List<Integer> list = new ArrayList<>();
                    list.add(productVal);
                    list.add(fre);
                    lists.add(list);
                } else {
                    lastList.set(1, lastList.get(1) + fre);
                }
            }
        }
        return lists;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值