题目描述
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 arrayencoded
. Eachencoded[i] = [vali, freqi]
describes theith
segment of repeated numbers innums
wherevali
is the value that is repeatedfreqi
times.
- For example,
nums = [1,1,1,2,2,2,2,2]
is represented by the run-length encoded arrayencoded = [[1,3],[2,5]]
. Another way to read this is "three1
's followed by five2
's".The product of two run-length encoded arrays
encoded1
andencoded2
can be calculated using the following steps:
- Expand both
encoded1
andencoded2
into the full arraysnums1
andnums2
respectively.- Create a new array
prodNums
of lengthnums1.length
and setprodNums[i] = nums1[i] * nums2[i]
.- Compress
prodNums
into a run-length encoded array and return it.You are given two run-length encoded arrays
encoded1
andencoded2
representing full arraysnums1
andnums2
respectively. Bothnums1
andnums2
have the same length. Eachencoded1[i] = [vali, freqi]
describes theith
segment ofnums1
, and eachencoded2[j] = [valj, freqj]
describes thejth
segment ofnums2
.Return the product of
encoded1
andencoded2
.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]].Example 2:
Input: encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]] Output: [[2,3],[6,1],[9,2]] Explanation: encoded1 expands to [1,1,1,2,3,3] and encoded2 expands to [2,2,2,3,3,3]. prodNums = [2,2,2,6,9,9], which is compressed into the run-length encoded array [[2,3],[6,1],[9,2]].Constraints:
1 <= encoded1.length, encoded2.length <= 105
encoded1[i].length == 2
encoded2[j].length == 2
1 <= vali, freqi <= 104
for eachencoded1[i]
.1 <= valj, freqj <= 104
for eachencoded2[j]
.- The full arrays that
encoded1
andencoded2
represent are the same length.
算法分析
a、[1,1,1,2,2,2,2,2]
b、[6,6,6,3,3,3,1,1]
c、[6,6,6,6,6,6,2,2]
d、[[6,6],[2,2]]
其中第一行是游程编码为[[1,3],[2,5]]的解码序列,而第二行是[[6,3],[3,3],[1,2]]的解码序列,第三行是这两个序列解码后的乘积。
题目要我们计算的,正是两个游程编码的序列按照位置进行乘积获得的序列,最后的结果按照游程编码进行压缩和输出,输出结果是d。
双指针
维持i和j两个指针分别指向encode1和encoded2
每次遍历取当前最短的长度,那么结果和长度就是新的结果,同时减去对应的长度
一旦出现长度为0,则递增对应的i和j
需要考虑乘法后相等情况,需要和当前结果最后数值比较,相等则直接加数字即可
代码
class Solution {
public:
vector<vector<int>> findRLEArray(vector<vector<int>>& encoded1, vector<vector<int>>& encoded2) {
std::vector<std::vector<int>> ans;
int i = 0;
int j = 0;
while(i < encoded1.size() && j < encoded2.size()) {
int common_freq = std::min(encoded1[i][1], encoded2[j][1]);
int mul_res = encoded1[i][0] * encoded2[j][0];
std::vector<int> new_pair = {mul_res, common_freq};
if(!ans.empty() && ans.back()[0] == mul_res) {
ans.back()[1] += common_freq;
} else {
ans.emplace_back(new_pair);
}
encoded1[i][1] -= common_freq;
encoded2[j][1] -= common_freq;
if(encoded1[i][1] == 0) {
++i;
}
if(encoded2[j][1] == 0) {
++j;
}
}
return ans;
}
};
时间复杂度分析
时间复杂度:两个数组length相等,O(n)
空间复杂度:O(1)