1868. Product of Two Run-Length Encoded Arrays
作者:互联网
My Solution:
class Solution { public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) { int i=0,j=0; List<int[]> list = new ArrayList<>(); while(i<encoded1.length && j<encoded2.length){ int k1 = encoded1[i][1]; int k2 = encoded2[j][1]; int prod = encoded1[i][0]*encoded2[j][0]; if(k1==k2){ list.add(new int[]{prod, k1}); i++; j++; }else if(k1<k2){ list.add(new int[]{prod, k1}); encoded2[j][1] = k2-k1; i++; }else if(k1>k2){ list.add(new int[]{prod, k2}); encoded1[i][1] = k1-k2; j++; } } List<List<Integer>> res = new ArrayList<>(); int[] last = list.get(0);int[] cur = null; for(int k=1;k<list.size();k++){ cur = list.get(k); if(last[0]==cur[0]){ cur[1]+=last[1]; last = cur; } else{ res.add(Arrays.asList(new Integer[]{last[0], last[1]})); last = cur; } } res.add(Arrays.asList(new Integer[]{cur[0], cur[1]})); return res; } }
标签:Product,encoded1,Run,k2,Arrays,list,List,int,new 来源: https://www.cnblogs.com/feiflytech/p/16133359.html