progchan

Textboard BBS.

  • No spam, doxxing, or illegal content.
  • Code blocks via [code]lang ... [/code].
  • For monospace just leave the language blank.
  • Tripcodes via name#secret.
  • Quote links with >>123.
Rules · 規則

Leet Code thread (2 posts)

339 Name: progchan !Hbv-BZDL : 2026-04-26 13:14:07

Dailies and Random Question or whatever else.

340 Name: progchan !Hbv-BZDL : 2026-04-26 13:15:32

https://leetcode.com/problems/zero-array-transformation-i/

Diff array stuff. This one was tough to conceptualize. If you actually loop through the L to R of each query it'll time you out for n*2 time complexity. So its actually like a presum where each index of diff represents the maximum you can subtract from the input array to try and get each element to zero.

class Solution:
    def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool:
        diff = [0] * (len(nums)+1)
        for l,r in queries:
            diff[l] += 1
            diff[r+1] -= 1
        max_diff = 0
        for i, n in enumerate(nums):
            max_diff += diff[i]
            if n - max_diff > 0:
                return False
        return True
Name: