Posted on

Description

Submission

class Solution {
public:
    int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {
        vector<int> counter(nums.size(), 0);

        for(int i = 0; i < requests.size(); ++i) {
            counter[requests[i][0]]++;
            if(requests[i][1] + 1 < nums.size())
                counter[requests[i][1]+1]--;
        }
        
        for(int i = 1; i < counter.size(); ++i) {
            counter[i] += counter[i-1];
        }
        
        sort(counter.begin(), counter.end());
        sort(nums.begin(), nums.end());

        long res = 0;
        for(int i = 0; i < counter.size(); ++i) {
            res += counter[i] * nums[i];
        }

        return res % long(1e9 + 7);
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *