# 1868. Product of Two Run-Length Encoded Arrays ###### tags: `Leetcode` `Medium` `FaceBook` Link: https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/ ## 思路 想法其实不难 用双指针,每次找目前指到的两个array里面最小的frequency,然后把乘积和最小frequency加到ans里面,然后这一个ans里面的两数乘积和上一个ans里面的两数乘积重复,就把上一个改掉 重点是想清楚怎么**优化代码!** ## Code ```java= class Solution { public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) { int idx1 = 0,idx2 = 0; List<List<Integer>> ans = new ArrayList<>(); while(idx1<encoded1.length && idx2<encoded2.length){ int curr1 = encoded1[idx1][1]; int curr2 = encoded2[idx2][1]; int res = encoded1[idx1][0]*encoded2[idx2][0]; int freq = Math.min(curr1,curr2); if(ans.isEmpty() || res!=ans.get(ans.size()-1).get(0)){ ans.add(Arrays.asList(res, freq)); } else{ ans.set(ans.size()-1, Arrays.asList(res, freq+ans.get(ans.size()-1).get(1))); } encoded1[idx1][1] -= freq; encoded2[idx2][1] -= freq; if(encoded1[idx1][1]==0)idx1++; if(encoded2[idx2][1]==0)idx2++; } return ans; } } ```