Skip to content

Commit

Permalink
feat: add lc 2772 greedy sliding window
Browse files Browse the repository at this point in the history
  • Loading branch information
Roytangrb committed Jan 4, 2024
1 parent fbdd930 commit bda2a60
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
- [[https://leetcode.com/problems/stone-game-iii/][Stone Game III]] ([[file:leetcode/python/1406-stone-game-iii.py][code]])
- [[https://leetcode.com/problems/string-compression-ii/][String Compression II]] ([[file:leetcode/python/1531-string-compression-ii.py][code]])
- Greedy
- [[https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero/][Apply Operations to Make All Array Elements Equal to Zero]] ([[file:leetcode/python/2772-apply-operations-to-make-all-array-elements-equal-to-zero.py][code]])
- [[https://leetcode.com/problems/break-a-palindrome/][Break a Palindrome]] ([[file:leetcode/python/1328-break-a-palindrome.py][code]])
- [[https://leetcode.com/problems/integer-to-roman/][Integer to Roman]] ([[file:leetcode/python/12-integer-to-roman.py][code]])
- [[https://leetcode.com/problems/jump-game-ii/][Jump Game II]] ([[file:leetcode/python/45-jump-game-ii.py][code]])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Author: RT
# Date: 2023-07-09T05:56:13.462Z
# URL: https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero/


class Solution:
def checkArray(self, nums: list[int], k: int) -> bool:
# accrued value need to be deleted from window ending at current index
# as we slide the window from left to right
curr = 0
n = len(nums)
for i in range(n):
if curr > nums[i]:
return False

nums[i], curr = nums[i] - curr, nums[i]
if i + 1 >= k:
curr -= nums[i - k + 1]

return curr == 0

0 comments on commit bda2a60

Please sign in to comment.