1868. Product of Two Run-Length Encoded Arrays

This problem can be resolved by two points, but the operation is pretty tricky:

    public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) {
        List<List<Integer>> res = new ArrayList<>();
        int count1 = 0, count2 = 0;
        int i = 0, j = 0;
        while (i < encoded1.length && j < encoded2.length) {
            int[] num1 = encoded1[i];
            int[] num2 = encoded2[j];
            int count = Math.min(num1[1], num2[1]);
            int product = num1[0] * num2[0];
            if (res.size() > 0 && res.get(res.size() - 1).get(0) == product) {
                List<Integer> temp = res.get(res.size() - 1);
                temp.set(1, temp.get(1) + count);   //list has a "set" function to set value at a index
                res.set(res.size() - 1, temp);
            } else {
                res.add(Arrays.asList(product, count));   //convert array to list
            }

            num1[1] -= count;
            if (num1[1] == 0) {
                i++;
            }
            num2[1] -= count;
            if (num2[1] == 0)
                j++;

        }

        return res;
    }

 

上一篇:【PHP数据结构】树和二叉树


下一篇:【题解】CF1536F Omkar and Akmar