|
94 | 94 |
|
95 | 95 | <!-- solution:start -->
|
96 | 96 |
|
97 |
| -### Solution 1 |
| 97 | +### Solution 1: Greedy + Difference Array + Priority Queue |
| 98 | + |
| 99 | +We want to "remove" as many interval queries as possible, while ensuring that for each position $i$, the number of selected queries covering it, $s(i)$, is at least the original array value $\textit{nums}[i]$, so that the value at that position can be reduced to 0 or below. If for some position $i$ we cannot satisfy $s(i) \ge \textit{nums}[i]$, it means that no matter how many more queries we select, it is impossible to make that position zero, so we return $-1$. |
| 100 | + |
| 101 | +To achieve this, we traverse the queries in order of their left endpoints and maintain: |
| 102 | + |
| 103 | +1. **Difference array** `d`: Used to record where the currently applied queries take effect—when we "apply" a query on the interval $[l, r]$, we immediately do `d[l] += 1` and `d[r+1] -= 1`. This way, when traversing to index $i$, the prefix sum tells us how many queries cover $i$. |
| 104 | +2. **Max heap** `pq`: Stores the right endpoints of the current "candidate" interval queries (store as negative numbers to simulate a max heap in Python's min heap). Why choose the "latest ending" interval? Because it can cover farther positions. Our greedy strategy is: **at each $i$, only pick the longest interval from the heap when necessary to increase coverage**, so that more intervals are available for subsequent positions. |
| 105 | + |
| 106 | +The specific steps are as follows: |
| 107 | + |
| 108 | +1. Sort `queries` by the left endpoint `l` in ascending order; |
| 109 | +2. Initialize the difference array `d` with length `n+1` (to handle the decrement at `r+1`), and set the current coverage count `s=0`, heap pointer `j=0`; |
| 110 | +3. For $i=0$ to $n-1$: |
| 111 | + |
| 112 | + - First, add `d[i]` to `s` to update the current coverage count; |
| 113 | + - Push all queries $[l, r]$ with left endpoint $\le i$ into the max heap `pq` (store `-r`), and advance `j`; |
| 114 | + - While the current coverage `s` is less than the required value `nums[i]`, and the heap is not empty, and the top interval in the heap still covers $i$ (i.e., $-pq[0] \ge i$): |
| 115 | + |
| 116 | + 1. Pop the top of the heap (the longest interval), which is equivalent to "applying" this query; |
| 117 | + 2. Increment `s` by 1 and do `d[r+1] -= 1` (so that after passing $r$, the coverage count automatically decreases); |
| 118 | + |
| 119 | + - Repeat the above steps until `s \ge nums[i]` or no more intervals can be selected; |
| 120 | + - If at this point `s < nums[i]`, it means it is impossible to make position $i$ zero, so return $-1$. |
| 121 | + |
| 122 | +4. After traversing all positions, the intervals remaining in the heap are those that were **not popped**, i.e., the queries that are truly **retained** (not used for the "zeroing" task). The heap size is the answer. |
| 123 | + |
| 124 | +The time complexity is $O(n + m \times \log m)$, and the space complexity is $O(n + m)$, where $n$ is the length of the array and $m$ is the number of queries. |
98 | 125 |
|
99 | 126 | <!-- tabs:start -->
|
100 | 127 |
|
|
0 commit comments