|
| 1 | +# [1868. Product of Two Run-Length Encoded Arrays](https://leetcode.com/problems/product-of-two-run-length-encoded-arrays) |
| 2 | + |
| 3 | +[中文文档](/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p><strong>Run-length encoding</strong> is a compression algorithm that allows for an integer array <code>nums</code> with many segments of <strong>consecutive repeated</strong> numbers to be represented by a (generally smaller) 2D array <code>encoded</code>. Each <code>encoded[i] = [val<sub>i</sub>, freq<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> segment of repeated numbers in <code>nums</code> where <code>val<sub>i</sub></code> is the value that is repeated <code>freq<sub>i</sub></code> times.</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li>For example, <code>nums = [1,1,1,2,2,2,2,2]</code> is represented by the <strong>run-length encoded</strong> array <code>encoded = [[1,3],[2,5]]</code>. Another way to read this is "three <code>1</code>s followed by five <code>2</code>s".</li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p>The <strong>product</strong> of two run-length encoded arrays <code>encoded1</code> and <code>encoded2</code> can be calculated using the following steps:</p> |
| 14 | + |
| 15 | +<ol> |
| 16 | + <li><strong>Expand</strong> both <code>encoded1</code> and <code>encoded2</code> into the full arrays <code>nums1</code> and <code>nums2</code> respectively.</li> |
| 17 | + <li>Create a new array <code>prodNums</code> of length <code>nums1.length</code> and set <code>prodNums[i] = nums1[i] * nums2[i]</code>.</li> |
| 18 | + <li><strong>Compress</strong> <code>prodNums</code> into a run-length encoded array and return it.</li> |
| 19 | +</ol> |
| 20 | + |
| 21 | +<p>You are given two <strong>run-length encoded</strong> arrays <code>encoded1</code> and <code>encoded2</code> representing full arrays <code>nums1</code> and <code>nums2</code> respectively. Both <code>nums1</code> and <code>nums2</code> have the <strong>same length</strong>. Each <code>encoded1[i] = [val<sub>i</sub>, freq<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> segment of <code>nums1</code>, and each <code>encoded2[j] = [val<sub>j</sub>, freq<sub>j</sub>]</code> describes the <code>j<sup>th</sup></code> segment of <code>nums2</code>.</p> |
| 22 | + |
| 23 | +<p>Return <i>the <strong>product</strong> of </i><code>encoded1</code><em> and </em><code>encoded2</code>.</p> |
| 24 | + |
| 25 | +<p><strong>Note:</strong> Compression should be done such that the run-length encoded array has the <strong>minimum</strong> possible length.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong>Example 1:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]] |
| 32 | +<strong>Output:</strong> [[6,6]] |
| 33 | +<strong>Explanation:</strong> encoded1 expands to [1,1,1,2,2,2] and encoded2 expands to [6,6,6,3,3,3]. |
| 34 | +prodNums = [6,6,6,6,6,6], which is compressed into the run-length encoded array [[6,6]]. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong>Example 2:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]] |
| 41 | +<strong>Output:</strong> [[2,3],[6,1],[9,2]] |
| 42 | +<strong>Explanation:</strong> encoded1 expands to [1,1,1,2,3,3] and encoded2 expands to [2,2,2,3,3,3]. |
| 43 | +prodNums = [2,2,2,6,9,9], which is compressed into the run-length encoded array [[2,3],[6,1],[9,2]]. |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong>Constraints:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li><code>1 <= encoded1.length, encoded2.length <= 10<sup>5</sup></code></li> |
| 51 | + <li><code>encoded1[i].length == 2</code></li> |
| 52 | + <li><code>encoded2[j].length == 2</code></li> |
| 53 | + <li><code>1 <= val<sub>i</sub>, freq<sub>i</sub> <= 10<sup>4</sup></code> for each <code>encoded1[i]</code>.</li> |
| 54 | + <li><code>1 <= val<sub>j</sub>, freq<sub>j</sub> <= 10<sup>4</sup></code> for each <code>encoded2[j]</code>.</li> |
| 55 | + <li>The full arrays that <code>encoded1</code> and <code>encoded2</code> represent are the same length.</li> |
| 56 | +</ul> |
| 57 | + |
| 58 | + |
| 59 | +## Solutions |
| 60 | + |
| 61 | +<!-- tabs:start --> |
| 62 | + |
| 63 | +### **Python3** |
| 64 | + |
| 65 | +```python |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +### **Java** |
| 70 | + |
| 71 | +```java |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +### **...** |
| 76 | + |
| 77 | +``` |
| 78 | +
|
| 79 | +``` |
| 80 | + |
| 81 | +<!-- tabs:end --> |
0 commit comments