Greedy: use query with furthest endpoint
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$$O(n\\log n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        multiset<int> pool;\n        multiset<int> used;\n        sort(queries.begin(), queries.end(), [](vector<int>& a, vector<int>& b) {\n            if (a[0] != b[0]) {\n                return a[0] < b[0];\n            }\n            return a[1] > b[1];\n        });\n\n        int cur = 0;\n        int j = 0;\n        int sol = 0;\n        for (int i = 0; i < nums.size(); i++) {\n            while (used.size() && *used.begin() == i) {\n                cur--;\n                used.erase(used.begin());\n            }\n            while (pool.size() && * pool.begin() == i) {\n                pool.erase(pool.begin());\n            }\n            while (cur < nums[i] && (j < queries.size() && queries[j][0] <= i || pool.size())) {\n                if (j < queries.size() && queries[j][0] == i && pool.size()) {\n                    auto p = *pool.rbegin();\n                    auto q = queries[j];\n                    if (p > q[1]) {\n                        used.insert(p);\n                        pool.erase(prev(pool.end()));\n                    } else {\n                        used.insert(q[1]+1);\n                        j++;\n                    }\n                } else if (pool.size()) {\n                    auto p = *pool.rbegin();\n                    used.insert(p);\n                    pool.erase(prev(pool.end()));\n                } else if (j < queries.size() && queries[j][0] <= i) {\n                    auto q = queries[j];\n                    used.insert(q[1]+1);\n                    j++;\n                }\n                cur++;\n                sol++;\n            }\n            if (cur < nums[i]) return -1;\n\n            while (j < queries.size() && queries[j][0] == i) {\n                pool.insert(queries[j][1]+1);\n                j++;\n            }\n                \n        }\n        return queries.size()-sol;\n    }\n};\n```
['C++']