Simple Solution | Sort && Priority Queue/Heap | C++
Intuition\n Describe your first thoughts on how to solve this problem. \nFor each index, need to find all queries that can meet: ri >= i and li <= i. In those q
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor each index, need to find all queries that can meet: ri >= i and li <= i. In those queries, find the one with farthest ri. Pick those queries, until nums[i] == 0; Then go to the next index;\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse the priority_queue/heap to keep all queries, and sort them by \'ri\' in descent order.\nUse the \'curr\' to record accumulation of all queries applied so far; and use the vector \'removes\' to record all points need to be decreased from \'curr\' in the future. Similar to basic line sweep.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n + mlog(m))$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n+m)$$\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        sort(queries.begin(), queries.end());\n        \n        int n = nums.size(), m = queries.size(), i = 0, j = 0, curr = 0;\n        vector<int> removes(n+1, 0);\n        priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int,int>>> pq;\n        int total = 0;\n        for(i = 0; i < n; i++) {\n            curr -= removes[i];\n            while(j < m && (queries[j][0] <= i && queries[j][1] >= i)) {\n                pq.push(make_pair(queries[j][1], queries[j][0]));\n                j++;\n            }\n            while(curr < nums[i]) {\n                if(pq.empty()) {\n                    return -1;\n                }\n                auto q = pq.top();\n                pq.pop();\n                if(q.first >= i) {\n                    curr++;\n                    total++;\n                    removes[q.first+1]++;\n                }\n            }\n        }\n        return m-total;\n    }\n};\n```
['C++']