Skip to content

Commit 7684774

Browse files
add 1868
1 parent af0f6ef commit 7684774

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|----------------------------------|----------------------------------------------------------------------
11-
| 3175 | [Find The First Player to win K Games in a Row](https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3175.java) | | Medium |
12-
| 3174 | [Clear Digits](https://leetcode.com/problems/clear-digits/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3174.java) | | Easy |
11+
| 3175 | [Find The First Player to win K Games in a Row](https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3175.java) | | Medium |
12+
| 3174 | [Clear Digits](https://leetcode.com/problems/clear-digits/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3174.java) | | Easy |
1313
| 3164 | [Find the Number of Good Pairs II](https://leetcode.com/problems/find-the-number-of-good-pairs-ii/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3164.java) | | Medium |
1414
| 3162 | [Find the Number of Good Pairs I](https://leetcode.com/problems/find-the-number-of-good-pairs-i/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3162.java) | | Easy |
1515
| 3131 | [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3131.java) | | Easy |
@@ -235,6 +235,7 @@ _If you like this project, please leave me a star._ ★
235235
| 1876 | [Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1876.java) || Easy | String |
236236
| 1874 | [Minimize Product Sum of Two Arrays](https://leetcode.com/problems/minimize-product-sum-of-two-arrays/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1874.java) || Medium | Array, Greedy, Sorting |
237237
| 1869 | [Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1869.java) || Easy | Array, Two Pointers |
238+
| 1868 | [Product of Two Run-Length Encoded Arrays](https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1868.java) || Medium | Two Pointers |
238239
| 1863 | [Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1863.java) || Easy | Backtracking, Recursion |
239240
| 1862 | [Sum of Floored Pairs](https://leetcode.com/problems/sum-of-floored-pairs/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1862.java) || Hard | Math |
240241
| 1861 | [Rotating the Box](https://leetcode.com/problems/rotating-the-box/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_1861.java) | [:tv:](https://youtu.be/2LRnTMOiqSI) | Medium | Array, Two Pointers |
@@ -787,7 +788,7 @@ _If you like this project, please leave me a star._ ★
787788
| 713 | [Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) || Medium |
788789
| 712 | [Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_712.java) | | Medium | DP
789790
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_709.java) | | Easy | String
790-
| 708 | [Insert into a Sorted Circular Linked List](https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_708.java) | | Medium | Linked List
791+
| 708 | [Insert into a Sorted Circular Linked List](https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_708.java) | | Medium | Linked List
791792
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_706.java) | | Easy | Design
792793
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_705.java) | | Easy | Design
793794
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_704.java) | [:tv:](https://youtu.be/eHVe_uyXeWg) | Easy | Binary Search
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class _1868 {
8+
public static class Solution1 {
9+
/**
10+
* Credit: https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/
11+
*/
12+
public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) {
13+
//local progress in the current intervals
14+
int pointer1 = 0;
15+
int pointer2 = 0;
16+
17+
//global progress in the overall encoded arrays
18+
int index1 = 0;
19+
int index2 = 0;
20+
21+
List<List<Integer>> result = new ArrayList<>();
22+
while (index1 < encoded1.length && index2 < encoded2.length) {
23+
int freq1 = encoded1[index1][1] - pointer1;
24+
int freq2 = encoded2[index2][1] - pointer2;
25+
//choose smaller one as the step size
26+
int freq = Math.min(freq1, freq2);
27+
28+
//update the local progress in the intervals
29+
pointer1 += freq;
30+
pointer2 += freq;
31+
32+
int product = encoded1[index1][0] * encoded2[index2][0];
33+
34+
int size = result.size();
35+
// if the current product is the same as the most recent one in the result, concatenate it
36+
if (size > 0 && result.get(size - 1).get(0) == product) {
37+
freq += result.get(size - 1).get(1);
38+
result.remove(size - 1);
39+
}
40+
result.add(Arrays.asList(product, freq));
41+
42+
//check if global progress is moving forward
43+
if (pointer1 == encoded1[index1][1]) {
44+
index1++;
45+
pointer1 = 0;
46+
}
47+
48+
if (pointer2 == encoded2[index2][1]) {
49+
index2++;
50+
pointer2 = 0;
51+
}
52+
}
53+
return result;
54+
}
55+
}
56+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1868;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class _1868Test {
12+
private static _1868.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _1868.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList(Arrays.asList(6, 6)), solution1.findRLEArray(new int[][]{{1, 3}, {2, 3}}, new int[][]{{6, 3}, {3, 3}}));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy