↓Skip to main content
  1. LeetCode/

LeetCode 930: Binary Subarrays With Sum

·2 mins· ·
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-20

  • Submissions: Accepted (first try)

Approach
#

As in LeetCode 1248: the array contains 0s, so adding a 0 leaves the sum unchanged and “sum exactly goal” is not monotonic. Subtract two “at most” counts instead.

When goal = 0, at_most(-1) must return 0 (the window cannot shrink to a negative length).

My Solution
#

class Solution:
    def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
        def at_most(limit: int) -> int:
            if limit < 0:
                return 0
            left = total = count = 0
            for right, x in enumerate(nums):
                total += x
                while total > limit:
                    total -= nums[left]
                    left += 1
                count += right - left + 1
            return count

        return at_most(goal) - at_most(goal - 1)

The prefix-sum version sums count[prefix - goal], exactly as in 1248.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • With goal = 0, special-case at_most(-1) to return 0.

Interview Explanation
#

With 0s in the array, “exactly goal” is not monotonic, so I compute “sum at most goal” minus “sum at most goal - 1”, each with a sliding window that adds right - left + 1 per right end. When goal is 0, the at-most-minus-one count is 0. Prefix sums with a hash map also work. Time O(n).

Related

LeetCode 2875: Minimum Size Subarray in Infinite Array
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 2875 note: strip target // sum full cycles; the remainder is smaller than one cycle, so a sliding window over two copies of nums always finds it
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 2958: Length of Longest Subarray With at Most K Frequency
·1 min
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2958 note: a longest-window slide that shrinks the left whenever the newly added number appears more than k times
LeetCode 2653: Sliding Subarray Beauty
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 note: values are only -50 to 50, so the fixed window keeps a count array, and each window accumulates upward from -50 to find the x-th smallest negative