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.
Example 1:
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]. At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
Example 2:
Input: nums = [1,1,2,3] Output: [1,3,3]
Constraints:
2 <= nums.length <= 100
nums.length % 2 == 0
1 <= nums[i] <= 100
program main
implicit none
integer, parameter :: n = 4
integer, dimension(n) :: nums
integer, dimension(n) :: expected
integer, dimension(:), allocatable :: actual
nums = [1, 2, 3, 4]
expected = [2, 4, 4, 4]
call test_case(nums, expected)
nums = [1, 1, 2, 3]
expected = [1, 3, 3]
call test_case(nums, expected)
contains
subroutine test_case(nums, expected)
implicit none
integer, dimension(:), intent(in) :: nums
integer, dimension(:), intent(in) :: expected
integer, dimension(:), allocatable :: actual
allocate(actual, source=decompress(nums))
if (size(actual) /= size(expected)) then
write (*, '("Test case failed: ", "Expected size:", i0, &
&" Actual size:", i0)') size(expected), size(actual)
stop 1
end if
if (any(actual /= expected)) then
write (*, '("Test case failed: ", "Expected:", *(i0, ","), &
&" Actual:", *(i0, ","))') expected, actual
stop 1
end if
deallocate(actual)
end subroutine test_case
function decompress(nums) result(decompressed)
implicit none
integer, dimension(:), intent(in) :: nums
integer, dimension(:), allocatable :: decompressed
integer :: i, j, k, freq, val
k = 0
do i = 1, size(nums), 2
freq = nums(i)
val = nums(i + 1)
do j = 1, freq
k = k + 1
decompressed(k) = val
end do
end do
end function decompress
end program main
temp.f95:14:4: 14 | expected = [1, 3, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 3)
module RunLengthEncoding
implicit none
contains
function decompress(nums) result(decompressed)
integer, intent(in) :: nums(:)
integer :: decompressed(size(nums)/2)
integer :: i, j, freq, val
i = 1
j = 1
do while (i <= size(nums))
freq = nums(i)
val = nums(i+1)
i = i + 2
do while (freq > 0)
decompressed(j) = val
j = j + 1
freq = freq - 1
end do
end do
end function decompress
end module RunLengthEncoding
program main
use RunLengthEncoding
implicit none
integer, parameter :: nums = [1, 2, 3, 4]
integer :: decompressed(size(nums)/2)
decompressed = decompress(nums)
write (*,*) 'Decompressed list: ', decompressed
end program main
temp.f95:27:28: 27 | integer, parameter :: nums = [1, 2, 3, 4] | 1 Error: Incompatible ranks 0 and 1 in assignment at (1) temp.f95:28:31: 28 | integer :: decompressed(size(nums)/2) | 1 Error: Symbol ‘nums’ is used before it is typed at (1) temp.f95:30:14: 30 | decompressed = decompress(nums) | 1 Error: Symbol ‘decompressed’ at (1) has no IMPLICIT type; did you mean ‘decompress’? temp.f95:30:32: 30 | decompressed = decompress(nums) | 1 Error: Symbol ‘nums’ at (1) has no IMPLICIT type
MOD = 10**9 + 7
def add_rooms(idx, children, memo):
if not children[idx]:
return 1
if memo[idx] != -1:
return memo[idx]
res = 1
cnt = 0
for child in children[idx]:
cnt += 1
res = (res * add_rooms(child, children, memo)) % MOD
for i in range(2, cnt + 1):
res = (res * i) % MOD
memo[idx] = res
return res
def num_of_ways(prev_room):
n = len(prev_room)
children = [[] for _ in range(n)]
for i in range(1, n):
children[prev_room[i]].append(i)
memo = [-1] * n
return add_rooms(0, children, memo)
#include <vector>
using namespace std;
const int MOD = 1e9 + 7;
int addRooms(int idx, const vector<vector<int>>& children, vector<int>& memo) {
if (children[idx].empty()) return 1;
if (memo[idx] != -1) return memo[idx];
long long res = 1;
int cnt = 0;
for (int child : children[idx]) {
cnt++;
res = (res * addRooms(child, children, memo)) % MOD;
}
for (int i = 2; i <= cnt; i++) {
res = (res * i) % MOD;
}
return memo[idx] = res;
}
int numOfWays(vector<int>& prevRoom) {
int n = prevRoom.size();
vector<vector<int>> children(n);
for (int i = 1; i < n; i++) {
children[prevRoom[i]].push_back(i);
}
vector<int> memo(n, -1);
return addRooms(0, children, memo);
}