Python3 - Two heaps
Keep track of 2 heaps.  The first one "endings" is all b from queries [a,b] which will tell you how far you can go and still decrement. Note that values in endi
Keep track of 2 heaps.  The first one "endings" is all `b` from queries `[a,b]` which will tell you how far you can go and still decrement. Note that values in endings are not used .. yet .. it\'s just where we store them.  At any time we actually need to use a query, we will take the one with the furthest possible ending to get the maximum benefit beyond this current round.\n\nThe second heap is "used" which is all queries we have actually used. Again we only need the second value.  If for example we are at index 5 then anything in used with an ending index >=5 wil allow us to decrease 1 from `n`.\n\nWith these heaps, simply loop through the array. Say you\'re at index `i`, anything before index `i` in used is no longer relevant so get rid of it.  Anything remaining lets you decrement `n` by 1.\nIf you did this and still need to decrement `n`, keep popping queries with the furthest possible ending whose start is `<=i`.  \nIf you use query `[a,b]` for some `a<=i` then you can push `i` onto `used` but you do have to subtract one from `ret` as you have now used another query. \n```\nclass Solution:\n    def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int:   \n        queries = sorted(queries)[::-1]\n        endings = []\n        heapq.heapify(endings)\n        used = []\n        heapq.heapify(used)\n\n        ret = len(queries)\n\n        for i,n in enumerate(nums):\n            while used and used[0]<i:\n                heapq.heappop(used)\n            n-=len(used)\n            \n            while queries and queries[-1][0]<=i:\n                heapq.heappush(endings, -queries.pop()[1])\n            \n            while n>0 and endings and -endings[0]>=i:\n                heapq.heappush(used,-heapq.heappop(endings))\n                ret-=1\n                n-=1\n            \n            if n>0: return -1\n        \n        return ret\n ```
[]