Problem solution · Python

Range Sum of Sorted Subarray Sums

Range Sum of Sorted Subarray Sums: a Python solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Binary search
Source
Kamyu LeetCode Solutions
Length
82 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

Binary search

For Range Sum of Sorted Subarray Sums, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 82 lines of Python from the credited upstream file range-sum-of-sorted-subarray-sums.py.
  • The implementation visibly relies on sequence storage, work queue.
  • No explicit loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

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

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeRange Sum of Sorted Subarray Sums · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlog(sum(nums)))# Space: O(n) # binary search + sliding window solutionclass Solution(object):    def rangeSum(self, nums, n, left, right):        """        :type nums: List[int]        :type n: int        :type left: int        :type right: int        :rtype: int        """        def countUntil(nums, target):            result, curr, left = 0, 0, 0            for right in xrange(len(nums)):                curr += nums[right]                while curr > target:                    curr -= nums[left]                    left += 1                result += right-left+1            return result                def sumUntil(nums, prefix, target):            result, curr, total, left = 0, 0, 0, 0            for right in xrange(len(nums)):                curr += nums[right]                total += nums[right]*(right-left+1)                while curr > target:                    curr -= nums[left]                    total -= prefix[right+1]-prefix[(left-1)+1]                    left += 1                result += total            return result                    def sumLessOrEqualTo(prefix, nums, left, right, count):            while left <= right:                mid = left + (right-left)//2                if countUntil(nums, mid)-count >= 0:                    right = mid-1                else:                    left = mid+1            return sumUntil(nums, prefix, left)-left*(countUntil(nums, left)-count)            MOD = 10**9+7        prefix = [0]*(len(nums)+1)        for i in xrange(len(nums)):            prefix[i+1] = prefix[i]+nums[i]        m, M = min(nums), sum(nums)        return (sumLessOrEqualTo(prefix, nums, m, M, right) -                sumLessOrEqualTo(prefix, nums, m, M, left-1))%MOD         # Time:  O(rlogr), worst: O(n^2 * logn)# Space: O(n)import heapq  # heap solutionclass Solution2(object):    def rangeSum(self, nums, n, left, right):        """        :type nums: List[int]        :type n: int        :type left: int        :type right: int        :rtype: int        """        MOD = 10**9+7        min_heap = []        for i, num in enumerate(nums, 1):            heapq.heappush(min_heap, (num, i))        result = 0        for i in xrange(1, right+1):            total, j = heapq.heappop(min_heap)            if i >= left:                result = (result+total)%MOD            if j+1 <= n:                heapq.heappush(min_heap, (total+nums[j], j+1))        return result 

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 ↗