[C++][Sort + Priority queue]
Intuition\n1. Sort queries\n2. for each index in nums, only add elible query in priority queue and pull the longest item from PQ first. \n\n# Code\ncpp []\nclas
# Intuition\n1. Sort queries\n2. for each index in nums, only add elible query in priority queue and pull the longest item from PQ first. \n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        auto l2 = [](vector<int> a, vector<int> b) {\n            return a[1]<b[1];\n        };\n        int usedQuery=0,sz=nums.size(),qIndex=0;;\n        sort(queries.begin(),queries.end());\n        priority_queue<vector<int>, vector<vector<int>>, decltype(l2)> pq(l2);\n\n        vector<int> prefixSum(sz+1);\n        for (int i=0;i<sz;i++){\n            if (i>0) prefixSum[i]+=prefixSum[i-1];\n            if (nums[i]==0 || nums[i]<=prefixSum[i]) continue;\n            while(!pq.empty() && pq.top()[1]<i){\n                pq.pop();\n            }\n           \n            while(qIndex<queries.size() && queries[qIndex][0]<=i){\n                pq.push(queries[qIndex++]);\n            }\n            \n            \n            for(int j=0;!pq.empty()&&prefixSum[i]<nums[i];j++){\n                if (pq.top()[1]>=i){\n                    prefixSum[i]+=1;\n                    prefixSum[pq.top()[1]+1]-=1;\n                    usedQuery++;\n                }\n                pq.pop();\n            }\n            if (prefixSum[i]<nums[i]) return -1;\n        }\n        return queries.size()-usedQuery;\n    }\n};\n```
['C++']