Skip to content

Commit 4b3e0ad

Browse files
add 1313
1 parent 8fb0ded commit 4b3e0ad

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ _If you like this project, please leave me a star._ ★
1212
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1313
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
1414
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
15+
|1313|[Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1313.java) | |Easy|Array|
1516
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
1617
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
1718
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1313. Decompress Run-Length Encoded List
8+
*
9+
* We are given a list nums of integers representing a list compressed with run-length encoding.
10+
* Consider each adjacent pair of elements [a, b] = [nums[2*i], nums[2*i+1]] (with i >= 0).
11+
* For each such pair, there are a elements with value b in the decompressed list.
12+
*
13+
* Return the decompressed list.
14+
*
15+
* Example 1:
16+
* Input: nums = [1,2,3,4]
17+
* Output: [2,4,4,4]
18+
* Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
19+
* The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
20+
* At the end the concatenation [2] + [4,4,4,4] is [2,4,4,4].
21+
*
22+
* Constraints:
23+
* 2 <= nums.length <= 100
24+
* nums.length % 2 == 0
25+
* 1 <= nums[i] <= 100
26+
* */
27+
public class _1313 {
28+
public static class Solution1 {
29+
public int[] decompressRLElist(int[] nums) {
30+
List<Integer> list = new ArrayList<>();
31+
for (int i = 0; i < nums.length - 1; i += 2) {
32+
int count = nums[i];
33+
int value = nums[i + 1];
34+
while (count-- > 0) {
35+
list.add(value);
36+
}
37+
}
38+
return list.stream().mapToInt(integer -> integer).toArray();
39+
}
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1313;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1313Test {
10+
private static _1313.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1313.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{2, 4, 4, 4}, solution1.decompressRLElist(new int[]{1, 2, 3, 4}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)