---
# System prepended metadata

title: 2772. Apply Operations to Make All Array Elements Equal to Zero
tags: [Leetcode, Sliding Window, Medium, Greedy]

---

Link: https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero/description/
## 思路
思路[参考](https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero/solutions/3739101/java-c-python-greedy-sliding-window/)
For the first ```A[i]>0```, we need to select the array ```A[i]```,```A[i+1]```..```A[i+k-1]```, and decrease all these elements by ```A[i]```

最后检查```cur==0```是因为
for example 碰到这样的testcase
nums = [0,45,82,98,99], k = 4
那么82,98,99都是消不下去的
## Code
```python=
class Solution:
    def checkArray(self, nums: List[int], k: int) -> bool:
        cur, n = 0, len(nums)
        for i in range(n):
            if nums[i]<cur:
                return False
            nums[i] -= cur
            cur += nums[i]
            if i-k+1>=0:
                cur -= nums[i-k+1]
        return cur==0
```