Problem solution · Python

Count of Range Sum

Count of Range Sum: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
74 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Count of Range Sum, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 74 lines of Python from the credited upstream file 327.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeCount of Range Sum · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def countRangeSum(self, nums: list[int], lower: int, upper: int) -> int:    n = len(nums)    self.ans = 0    prefix = list(itertools.accumulate(nums, initial=0))     self._mergeSort(prefix, 0, n, lower, upper)    return self.ans   def _mergeSort(      self,      prefix: list[int],      l: int,      r: int,      lower: int,      upper: int,  ) -> None:    if l >= r:      return     m = (l + r) // 2    self._mergeSort(prefix, l, m, lower, upper)    self._mergeSort(prefix, m + 1, r, lower, upper)    self._merge(prefix, l, m, r, lower, upper)   def _merge(      self,      prefix: list[int],      l: int,      m: int,      r: int,      lower: int,      upper: int,  ) -> None:    lo = m + 1  # the first index s.t. prefix[lo] - prefix[i] >= lower    hi = m + 1  # the first index s.t. prefix[hi] - prefix[i] > upper     # For each index i in range [l, m], add hi - lo to `ans`.    for i in range(l, m + 1):      while lo <= r and prefix[lo] - prefix[i] < lower:        lo += 1      while hi <= r and prefix[hi] - prefix[i] <= upper:        hi += 1      self.ans += hi - lo     sorted = [0] * (r - l + 1)    k = 0  # sorted's index    i = l  # left's index    j = m + 1  # right's index     while i <= m and j <= r:      if prefix[i] < prefix[j]:        sorted[k] = prefix[i]        k += 1        i += 1      else:        sorted[k] = prefix[j]        k += 1        j += 1     # Put the possible remaining left part into the sorted array.    while i <= m:      sorted[k] = prefix[i]      k += 1      i += 1     # Put the possible remaining right part into the sorted array.    while j <= r:      sorted[k] = prefix[j]      k += 1      j += 1     prefix[l:l + len(sorted)] = sorted 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗