-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompressRunLengthList.java
More file actions
46 lines (40 loc) · 1.79 KB
/
decompressRunLengthList.java
File metadata and controls
46 lines (40 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*1313. Decompress Run-Length Encoded List
* We are given a list nums of integers representing a list compressed with run-length encoding.
* Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
* Return the decompressed list.
*
* Input: nums = [1,2,3,4]
* Output: [2,4,4,4]
* Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
*/
import java.util.*;
public class decompressRunLengthList {
public static int[] decompress(int[] nums){
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0; i<nums.length; i=i+2){
int freq = nums[i];
int value = nums[i+1];
for(int j=0; j<freq; j++){
list.add(value);
}
}
//convert arraylist to int[]
int[] resultArr = new int[list.size()];
for(int i=0; i<list.size(); i++){
resultArr[i] = list.get(i);
}
return resultArr;
}
public static void main(String[] args) {
int[] nums = {1,2,3,4};
int[] result = decompress(nums);
for(int element : result){
System.out.print(element + " ");
}
}
}
/*Note: Since we require size of array at initial stage i.e., at the time of initializing the array. So we cant use that in this problem. Therefore we use an appraoch by the use of ArrayList which provides an advantage over array.
And there is direct method to convert ArrayList -> int[].
.toArray -> converts an ArrayList to Integer[] not int[]
*/