6 line Python code, this is an easy question if you know how to use bisect in Python.


  • 1
    Y
    class Solution(object):
        def countRangeSum(self, nums, lower, upper):
            """
            :type nums: List[int]
            :type lower: int
            :type upper: int
            :rtype: int
            """
            res, cur_sum, sum_list = 0, 0, [0, float("inf")]
            for n in nums:
                cur_sum += n
                res += bisect.bisect_right(sum_list, cur_sum-lower) - bisect.bisect_left(sum_list, cur_sum-upper)
                bisect.insort(sum_list, cur_sum)  
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.