LeetCode 1868. Product of Two Run-Length Encoded Arrays

原题链接在这里:https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/description/

题目:

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]].

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 each encoded1[i].
  • 1 <= valj, freqj <= 104 for each encoded2[j].
  • The full arrays that encoded1 and encoded2 represent are the same length.

题解:

Using two pointers. i points to current index of encoded1 and j points to current index of encoded2.

The product of two current value, the length should be the minimum of the current segments of encoded1 and encoded2.

When adding the current product to res, check the last list of segment and if its value == current value, extend the last segment's length.

Otherwise, add a new segment.

Move the pointers. Deduct current minimum length for both, whichever current length is 0, move the pointer to the next position.

Time Complexity: O(m + n). m = encoded1.length, n = endcoded2.length.

Space: O(1). regardless res.

AC Java:

 1 class Solution {
 2     public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) {
 3         List<List<Integer>> res = new ArrayList<>();
 4         if(encoded1 == null || encoded1.length == 0 || encoded2 == null || encoded2.length == 0){
 5             return res;
 6         }
 7 
 8         int i = 0;
 9         int j = 0;
10         while(i < encoded1.length && j < encoded2.length){
11             int len = Math.min(encoded1[i][1], encoded2[j][1]);
12             int prd = encoded1[i][0] * encoded2[j][0];
13             if(res.size() > 0 && res.get(res.size() - 1).get(0) == prd){
14                 List<Integer> last = res.get(res.size() - 1);
15                 last.set(1, last.get(1) + len);
16             }else{
17                 res.add(Arrays.asList(prd, len));
18             }
19 
20             encoded1[i][1] -= len;
21             encoded2[j][1] -= len;
22             if(encoded1[i][1] == 0){
23                 i++;
24             }
25 
26             if(encoded2[j][1] == 0){
27                 j++;
28             }
29         }
30 
31         return res;
32     }
33 }

 

posted @ 2024-03-31 00:46  Dylan_Java_NYC  阅读(22)  评论(0编辑  收藏  举报