priority queue solution + greedy (c++)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        int n = nums.size();\n        int m = queries.size();\n        unordered_map<int, int> mp;\n        unordered_map<int, int> mp1;\n\n        \n        for (int i = 0; i < m; i++) {\n            mp[queries[i][0]]++;\n            mp1[queries[i][1]]++;\n        }\n\n        priority_queue<int> pq; \n        unordered_map<int, vector<int>> temp;\n\n        \n        for (int i = 0; i < m; i++) {\n            temp[queries[i][0]].push_back(queries[i][1]);\n        }\n\n        multiset<int> m2; \n        int overlaps = 0, newOverlaps = 0, ans = 0, lastUsed = 0;\n\n        for (int i = 0; i < n; i++) {\n           \n            if (mp.find(i) != mp.end()) {\n                overlaps += mp[i];\n                newOverlaps += mp[i];\n                for (int end : temp[i]) {\n                    pq.push(end);\n                }\n            }\n\n            if (nums[i] == 0) {\n                \n                //continue;\n            } else if (nums[i] - overlaps == 0) {\n                \n                int required = nums[i] - lastUsed;\n                if (required > 0) {\n                    ans += required;\n                    int x = 0;\n                    while (x < required) {\n                        if (pq.empty()) return -1; \n                        int y = pq.top();\n                        pq.pop();\n                        if (y >= i) {\n                            m2.insert(y);\n                        }\n                        x++;\n                    }\n                    lastUsed += required;\n                }\n            } else if (nums[i] - overlaps < 0) {\n               \n                if (lastUsed < nums[i]) {\n                    int required = nums[i] - lastUsed;\n                    int x = 0;\n                    while (x < required) {\n                        if (pq.empty()) return -1; \n                        int y = pq.top();\n                        pq.pop();\n                        if (y >= i) {\n                            m2.insert(y);\n                        }\n                        x++;\n                    }\n                    ans += required;\n                    lastUsed += required;\n                }\n            }\n\n           \n            if (nums[i] > overlaps) return -1;\n\n            \n            if (mp1.find(i) != mp1.end()) {\n                overlaps -= mp1[i];\n\n              \n                m2.erase(i);\n\n                \n                lastUsed = m2.size();\n            }\n            //cout<<overlaps<<" "<<lastUsed<<endl;\n        }\n\n        return m - ans;\n    }\n};\n\n```
['C++']