Python Medium
```python3 []
class Solution:
    def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int:
        '''
        can we greedily sort by the largest?
        
        we need a O(nlogn) solution or better
        
        things that come to mind:
        difference array
        segment Tree
        dynamic programming
        
        greedy
        sorting queries based on size
        
        union find
        
        we iterate left to right in nums:
        at current index, while it isnt covered, we look at unused queries that have the latest endpoint and start at current index or left of it
        
        (1, 3), (2, 5)
        
        if we are at index 1 and need an interval, we take (1, 3)
        
        in other cases, we take 2, 5
    
        
        heap
        '''
        N = len(nums)
        Q = len(queries)
        
        difference_arr = [0] * (N + 1)
        
        h = []
        queries.sort()
        q = deque(queries)
        used = 0
        
        
        cur = 0
        
        for index, x in enumerate(nums):
            while q and q[0][0] <= index:
                l, r = q.popleft()
                heapq.heappush(h, -r)
                
      
                
                
            cur += difference_arr[index]
            
            while h and cur < x:
                r = heapq.heappop(h) * -1
                if r < index:
                    return -1
                cur += 1
                used += 1
                difference_arr[r + 1] -= 1
                
            if cur < x:
                return -1
            
            

            
        return Q - used
            
        
        
        
        
        
        
```
['Python3']