Iterators using 2 heaps | 42ms
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```rust []
impl Solution {
    pub fn max_removal(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
        use std::collections::{VecDeque, BinaryHeap};
        use std::cmp::Reverse;

        let mut queries = VecDeque::from(queries);
        queries.make_contiguous().sort_unstable();

        let mut avail = BinaryHeap::<usize>::new();
        let mut working = BinaryHeap::<Reverse<usize>>::new();

        let found = nums
          .into_iter()
          .enumerate()
          .scan(true, |f, (i, n)| {
            if ! *f {
                return None;
            }
            while queries.front().is_some_and(|q| q[0] as usize <= i) {
                avail.push(queries.pop_front().unwrap()[1] as usize);
            }
            while working.peek().is_some_and(|&Reverse(end)| end < i) {
                working.pop();
            }
            while n as usize > working.len() {
                if avail.peek().is_some_and(|&end| end >= i) {
                    working.push(Reverse(avail.pop().unwrap()));
                } else {
                    *f = false;
                    return Some(false);
                }
            }
            Some(true)
          })
          .last()
          .unwrap();
        
        if !found {-1} else {avail.len() as i32}
    }
}
```
['Rust']