[Java] Track cumulative effect and use PriorityQueue
Approach\n\n1. Let\'s sort the queries by starting point ascending and by ending point descending.\n2. Let\'s say we iterate over nums array. At the point i we 
# Approach\n\n1. Let\'s sort the queries by starting point ascending and by ending point descending.\n2. Let\'s say we iterate over nums array. At the point $$i$$ we have an option to cover it with some queries. Let\'s greedy pick the query with farthest ending point to minimize the query usage.\n3. How to understand that the nums[i] is covered? Let\'s keep track of cumulative effect by used queries. To update this array faster we update it similar to prefix sum at every index.\n\nCheck the code comments for more insights.\n\n# Complexity\n- Time complexity:\n$$O(n*log(n) + m)$$\n\n- Space complexity:\n$$O(m + n)$$\n\n# Code\n```java []\nclass Solution {\n    public int maxRemoval(int[] nums, int[][] queries) {\n        // need to iterate over queries with starting point asc\n        Arrays.sort(queries, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);\n        int[] effect = new int[nums.length + 1];\n        int res = 0;\n        // here we store active queries which we can use to cover nums[i]\n        Queue<int[]> pq = new PriorityQueue<>((a, b) -> b[1] - a[1]);\n        int j = 0; // queries index\n        // now for each index\n        for (int i = 0; i < nums.length; i++) {\n            // queries we can no longer use\n            while (!pq.isEmpty() && pq.peek()[1] < i) {\n                pq.poll();\n            }\n\n            // same here, moving j over unused queries\n            while (j < queries.length && queries[j][1] < i) {\n                j++;\n            }\n\n            // while our cumulative effect is less than the effect we need\n            while (effect[i] < nums[i]) {\n                boolean used = false;\n                // all the queries we can potentially use go to queue\n                while (j < queries.length && queries[j][0] <= i) {\n                    pq.add(queries[j]);\n                    j++;\n                }\n                // why we can use some query\n                if (!pq.isEmpty()) {\n                    int[] tmp = pq.poll();\n                    // can\'t use it - went over its ending\n                    if (tmp[1] < i) {\n                        continue;\n                    }\n                    // now let\'s add cumulative effect\n                    effect[i]++;\n                    // at this point query ends and we need to take back the effect\n                    effect[tmp[1] + 1]--;\n                    res++; // using the query\n                    used = true;\n                }\n                // if we can\'t cover the i - we\'re out\n                if (!used) {\n                    return -1;\n                }\n            }\n            // works as prefix sum to cover the ending queries\n            effect[i + 1] += effect[i];\n        }\n        // everything unused is what we can throw off\n        return queries.length - res;\n    }\n}\n```
['Dynamic Programming', 'Greedy', 'Heap (Priority Queue)', 'Java']