Easy solution with two heaps and greedy algorithm to solve in 143ms with C++ beat 100%
Intuition\nTo maximize utilization of query, each time we need to choose the query which have farthest ending point.\n\n# Approach\nThere is two heaps. \nOne is
# Intuition\nTo maximize utilization of query, each time we need to choose the query which have farthest ending point.\n\n# Approach\nThere is two heaps. \nOne is max heap to record the candidate who can be use at index i. (Record its ending point)\nAnother one is min heap to record the query\'s ending point which we actually choose it to minus the nums.\n\n**Approach** \nWhen we trace to the index i, w\n1. We will put the query j into the candidate first when the query j\'s starting point is touched.\n2. [Greedy] We choose the query with maximum ending point in the candidate to add **cur** parameter until cur == val. (Because choosing any other query will reduce the impact on future indexes) And also record the choosing query\'s ending point to the end heap.\n3. Early return when cur < val because there is no enough query to reduce the nums[i].\n4. Pop the query if the ending point is at i.\n\n\n# Complexity\n- Time complexity:\nO(n+ mlogm), n is nums size, m is queries size\n\n- Space complexity:\nO()\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        sort(queries.begin(), queries.end());\n        int ind = 0;\n        int n = nums.size();\n        int cur = 0; // current index minus value\n        priority_queue<int> cand; \n        priority_queue<int, vector<int>, greater<int>> end;\n        for(int i=0; i<n; ++i){\n            int val = nums[i];\n            // put query into candidate\n            while(ind < queries.size() && queries[ind][0] == i){\n                cand.push(queries[ind][1]);\n                ++ind;\n            }\n            // Use the candidate to be minus value\n            while(cur < val && !cand.empty() && cand.top() >= i){\n                ++cur;\n                end.push(cand.top());\n                cand.pop();\n            }\n            if(cur < val)\n                return -1;  // does not have enough candidate\n            // remove the minus value when touch the end\n            while(!end.empty() && end.top() == i){\n                --cur;\n                end.pop();\n            }\n        }\n        return cand.size();\n    }\n};\n```
['C++']