[Java/C++/Python] Take the query ending latest
Intuition\nItearte A,\nif we need more query,\ngreedily take the available query with biggest ending index.\n\n# Explanation\nh are ending indices of available 
# **Intuition**\nItearte `A`,\nif we need more query,\ngreedily take the available query with biggest ending index.\n\n# **Explanation**\n`h` are ending indices of available queries\n`cur` are number of queries currently in use.\n\nIterate each `A[i]`,\npopping out all ended query,\nupdate availble queries in `h`.\n\nWhile valid queries `cur` is smaller than required `A[i]`,\nwe need to take a query from `h` and add to `cur`.\n**the stradgy is popping out the query with biggest ending index.**\n\nRepeat this process and return the number of unused queries.\n<br>\n\n# **Complexity**\nTime `O(n + qlogq)`\nSpace `O(q)`\n<br>\n\n```Java [Java]\n    public int maxRemoval(int[] A, int[][] queries) {\n        int n = A.length, nq = queries.length;\n        Arrays.sort(queries, (a, b) -> a[0] - b[0]);\n        PriorityQueue<Integer> h = new PriorityQueue<>(); // min heap\n        int[] end = new int[n + 1];\n        int cur = 0, j = 0;\n        for (int i = 0; i < n; ++i) {\n            cur -= end[i];\n            while (j < nq && queries[j][0] <= i) {\n                h.offer(-queries[j][1]); // Add negative for min-heap\n                j++;\n            }\n            while (cur < A[i]) {\n                if (h.isEmpty() || -h.peek() < i) {\n                    return -1;\n                }\n                end[-h.poll() + 1]++;\n                cur++;\n            }\n        }\n        return h.size();\n    }\n```\n```C++ [C++]\n    int maxRemoval(vector<int>& A, vector<vector<int>>& queries) {\n        int n = A.size(), nq = queries.size();\n        sort(queries.begin(), queries.end());\n        priority_queue<int> h; // max heap\n        vector<int> end(n + 1, 0);\n        int cur = 0, j = 0;\n        for (int i = 0; i < n; ++i) {\n            cur -= end[i];\n            while (j < nq && queries[j][0] <= i) {\n                h.push(queries[j][1]);\n                j++;\n            }\n            while (cur < A[i]) {\n                if (h.empty() || h.top() < i) {\n                    return -1;\n                }\n                end[h.top() + 1]++;\n                h.pop();\n                cur++;\n            }\n        }\n        return h.size();\n    }\n```\n```py [Python3]\n    def maxRemoval(self, A: List[int], queries: List[List[int]]) -> int:\n        queries = sorted(queries)[::-1]\n        ava = SortedList()\n        cur = SortedList()\n        for i in range(len(A)):\n            while queries and queries[-1][0] <= i:\n                ava.add(queries.pop()[1])\n            while cur and cur[0] < i:\n                cur.pop(0)\n            while A[i] > len(cur):\n                if not ava or ava[-1] < i:\n                    return -1\n                cur.add(ava.pop())\n        return len(ava)\n```
['C', 'Python', 'Java']