A swift solution
IntuitionApproachComplexity

Time complexity:
O((n + q) * log q)


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:
O((n + q) * log q)

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

# Code
```swift []
class Solution {
    func maxRemoval(_ nums: [Int], _ queries: [[Int]]) -> Int {
        var queries = queries.sorted(by: { $0[0] < $1[0] })
        var m = queries.count
        var n = nums.count
        var nums = nums

        var maxHeap = PriorityQueue<Int>(sort: >)
        var minHeap = PriorityQueue<Int>(sort: <)

        var used = 0
        var j = 0

        for i in 0..<n {
            while j < m && queries[j][0] == i {
                maxHeap.enqueue(queries[j][1])
                j += 1
            }

            nums[i] -= minHeap.count

            while nums[i] > 0, let top = maxHeap.peek(), top >= i {
                minHeap.enqueue(maxHeap.dequeue()!)
                used += 1
                nums[i] -= 1
            }

            if nums[i] > 0 {
                return -1
            }

            while let top = minHeap.peek(), top == i {
                minHeap.dequeue()
            } 
        }

        return m - used
    }
}

// Priority Queue Implementation
struct PriorityQueue<T: Comparable> {
    private var elements: [T] = []
    private let sort: (T, T) -> Bool

    init(sort: @escaping (T, T) -> Bool) {
        self.sort = sort
    }

    var count: Int {
        return elements.count
    }

    var isEmpty: Bool {
        return elements.isEmpty
    }

    func peek() -> T? {
        return elements.first
    }

    mutating func enqueue(_ element: T) {
        elements.append(element)
        siftUp(from: elements.count - 1)
    }

    mutating func dequeue() -> T? {
        guard !elements.isEmpty else { return nil }
        elements.swapAt(0, elements.count - 1)
        let removed = elements.removeLast()
        siftDown(from: 0)
        return removed
    }

    mutating func remove(_ value: T) {
        if let index = elements.firstIndex(of:  value) {
          elements.swapAt(index, elements.count - 1)
          elements.removeLast()
          if index < elements.count {
                siftUp(from: index)
                siftDown(from: index)
            }
        }
    }

    private mutating func siftUp(from index: Int) {
        var child = index
        var parent = (child - 1) / 2
        while child > 0 && sort(elements[child], elements[parent]) {
            elements.swapAt(child, parent)
            child = parent
            parent = (child - 1) / 2
        }
    }

    private mutating func siftDown(from index: Int) {
        var parent = index
        let count = elements.count
        while true {
            let left = 2 * parent + 1
            let right = 2 * parent + 2
            var candidate = parent

            if left < count && sort(elements[left], elements[candidate]) {
                candidate = left
            }
            if right < count && sort(elements[right], elements[candidate]) {
                candidate = right
            }
            if candidate == parent {
                return
            }
            elements.swapAt(parent, candidate)
            parent = candidate
        }
    }
}

```
['Swift']