diff options
author | Jack Sangdahl <[email protected]> | 2025-05-19 23:02:14 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-19 23:02:14 -0600 |
commit | efde03c42facb93ae6dcefb0ba795463fed77f72 (patch) | |
tree | 8eeb2f0db0bc406fe38b64ac77d9caef8404f58f | |
parent | c585422d643252924194811d6204f773a35ca5e0 (diff) | |
download | leetcode-efde03c42facb93ae6dcefb0ba795463fed77f72.tar.gz leetcode-efde03c42facb93ae6dcefb0ba795463fed77f72.zip |
3355. zero array transform 1
-rw-r--r-- | zero_array_transformation1.c | 45 | ||||
-rw-r--r-- | zero_array_transformation1.py | 36 |
2 files changed, 81 insertions, 0 deletions
diff --git a/zero_array_transformation1.c b/zero_array_transformation1.c new file mode 100644 index 0000000..45f388c --- /dev/null +++ b/zero_array_transformation1.c @@ -0,0 +1,45 @@ +// 3355. Zero Array Transformation I +#include "leetcode.h" + +/* + * you are given an integer array 'nums' of length 'n' and a 2d array 'queries' + * where 'queries[i] = [li, ri]'. for each 'queries[i]' select a subset of + * indices within the range '[li, ri]' in 'nums' and decrement the values at the + * selected indices by 1. a zero array is an array where all elements are equal + * to 0. return true if it is possible to transform 'nums' into a zero array + * after processing all the queries sequentially, otherwise return false. + */ + +bool isZeroArray(int *nums, int numsSize, int **queries, int queriesSize, + int *queriesColSize) { + int *freq = (int *)calloc(numsSize + 1, sizeof(int)); + for (int i = 0; i < queriesSize; i++) { + int l = queries[i][0], r = queries[i][1]; + freq[l]++; + freq[r + 1]--; + } + int cnt = 0; + for (int i = 0; i < numsSize; i++) { + cnt += freq[i]; + if (nums[i] > cnt) { + free(freq); + return false; + } + } + free(freq); + return true; +} + +int main() { + int n1[] = {1, 0, 1}, q1i[1][2] = {{0, 2}}; + int n2[] = {4, 3, 2, 1}, q2i[2][2] = {{1, 3}, {0, 2}}; + struct two_d_arr q1, q2; + two_d_arr_init(&q1, ARRAY_SIZE(q1i), ARRAY_SIZE(q1i[0]), q1i); + two_d_arr_init(&q2, ARRAY_SIZE(q2i), ARRAY_SIZE(q2i[0]), q2i); + printf("%d\n", isZeroArray(n1, ARRAY_SIZE(n1), q1.arr, q1.row_size, + q1.col_size)); // expect: 1 + printf("%d\n", isZeroArray(n2, ARRAY_SIZE(n2), q2.arr, q2.row_size, + q2.col_size)); // expect: 0 + two_d_arr_free(&q1); + two_d_arr_free(&q2); +} diff --git a/zero_array_transformation1.py b/zero_array_transformation1.py new file mode 100644 index 0000000..e4cc270 --- /dev/null +++ b/zero_array_transformation1.py @@ -0,0 +1,36 @@ +# 3355. Zero Array Transformation I + +""" +you are given an integer array 'nums' of length 'n' and a 2d array 'queries' +where 'queries[i] = [li, ri]'. for each 'queries[i]' select a subset of +indices within the range '[li, ri]' in 'nums' and decrement the values at the +selected indices by 1. a zero array is an array where all elements are equal +to 0. return true if it is possible to transform 'nums' into a zero array +after processing all the queries sequentially, otherwise return false. +""" + + +class Solution(object): + def isZeroArray(self, nums, queries): + """ + :type nums: List[int] + :type queries: List[List[int]] + :rtype: bool + """ + n = len(nums) + freq = [0] * (n + 1) + for l, r in queries: + freq[l] += 1 + freq[r + 1] -= 1 + cnt = 0 + for i, j in enumerate(nums): + cnt += freq[i] + if j > cnt: + return False + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.isZeroArray(nums=[1, 0, 1], queries=[[0, 2]])) + print(obj.isZeroArray(nums=[4, 3, 2, 1], queries=[[1, 3], [0, 2]])) |