LeetCode solutions

2394. Count Subarrays With Score Less Than K

My accepted Python solution to LeetCode problem 2394, Count Subarrays With Score Less Than K, running in 103ms.

  • Difficulty: Hard
  • Python
  • Runtime 103ms
  • Memory 28.8MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 103ms, memory 28.8MB, accepted 2026-01-01.

python
class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        n = len(nums)
        result = 0
        left = 0
        curr_sum = 0
        
        for right in range(n):
            curr_sum += nums[right]
            
            while curr_sum * (right - left + 1) >= k:
                curr_sum -= nums[left]
                left += 1
            
            result += right - left + 1
        
        return result

Source