Leetcode 327 Solution

This article provides solution to leetcode question 327 (count-of-range-sum)

https://leetcode.com/problems/count-of-range-sum

Solution

class Solution {
public:
    int countRangeSum(vector<int>& nums, int lower, int upper) {
        multiset<int64_t> sums;
        sums.insert(0);

        int64_t sum = 0;
        int res = 0;
        for (auto num : nums)
        {
            sum += num;
            res += distance(sums.lower_bound(sum - upper), sums.upper_bound(sum - lower));
            sums.insert(sum);
        }

        return res;
    }
};