↓Skip to main content
  1. LeetCode/

LeetCode 2302: Count Subarrays With Score Less Than K

·2 mins· ·
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search 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: hard First Attempt: 2026-01-18

  • Submissions: Accepted (first try)

Approach
#

score = sum × length, and every number is positive, so a larger window has a larger sum and length: the score increases monotonically.

Use the counting sliding window:

  • Add on the right; shrink the left while the score is >= k.
  • There are right - left + 1 valid subarrays ending at right.

My Solution
#

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        left = total = count = 0
        for right, x in enumerate(nums):
            total += x
            while total * (right - left + 1) >= k:
                total -= nums[left]
                left += 1
            count += right - left + 1
        return count

left may pass right (a single element already scores >= k); then right - left + 1 is 0, the window is empty, and the next round starts again from left.

Complexity
#

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

Mistakes to Avoid
#

  • The problem says strictly less than k, so shrink while >= k.
  • Brute-force enumeration: n reaches 10^5.

Interview Explanation
#

The score is sum times length with positive numbers, so it grows with the window. I slide a window that shrinks while the score is at least k, and add the right - left + 1 valid subarrays ending at each right end. Time O(n).

Related

LeetCode 2968: Apply Operations to Maximize Frequency Score
·2 mins
LeetCode Hard Sliding-Window Sorting Prefix-Sum Binary-Search Array
LeetCode 2968 note: after sorting, the cheapest common target for a block is its median; prefix sums give the cost in O(1), and it grows with the window, so slide
LeetCode 2106: Maximum Fruits Harvested After at Most K Steps
·2 mins
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search Array
LeetCode 2106 note: the harvest is always a contiguous block of fruits; covering [L, R] takes (R - L) + min(|start - L|, |R - start|) steps, so slide a window over the fruits array
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 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2009: Minimum Number of Operations to Make Array Continuous
·2 mins
LeetCode Hard Sliding-Window Hash-Table Binary-Search Array
LeetCode 2009 note: the result is n consecutive integers; after deduplicating and sorting, a sliding window finds the most distinct values within a span of n - 1, and the rest must change