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 /zero_array_transformation1.py | |
parent | c585422d643252924194811d6204f773a35ca5e0 (diff) | |
download | leetcode-efde03c42facb93ae6dcefb0ba795463fed77f72.tar.gz leetcode-efde03c42facb93ae6dcefb0ba795463fed77f72.zip |
3355. zero array transform 1
Diffstat (limited to 'zero_array_transformation1.py')
-rw-r--r-- | zero_array_transformation1.py | 36 |
1 files changed, 36 insertions, 0 deletions
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]])) |