LeetCode solutions

3639. Zero Array Transformation I

My accepted Python solution to LeetCode problem 3639, Zero Array Transformation I, running in 63ms.

  • Difficulty: Medium
  • Python
  • Runtime 63ms
  • Memory 52.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 63ms, memory 52.9MB, accepted 2025-12-31.

python
class Solution:
    def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool:
        n = len(nums)
        # For each position, count how many queries cover it
        # If count >= nums[i] for all i, we can make it zero
        
        # Use difference array for counting coverage
        diff = [0] * (n + 1)
        for l, r in queries:
            diff[l] += 1
            diff[r + 1] -= 1
        
        # Compute prefix sum to get coverage at each position
        coverage = 0
        for i in range(n):
            coverage += diff[i]
            if coverage < nums[i]:
                return False
        
        return True

Source