Monitoring~45 min2s · 256 MBtarget O(n log n) time, O(n) spaceclassic twin: lc-218segment-tree
A monitoring dashboard has a status banner. Each alert is open for a window of time and carries a priority score. The banner shows the highest score among the alerts that are open at that moment, and it shows 0 when none is open.
Your team lead asks: "At which moments does the banner change, and what does it change to?" A day has up to 10,000 alert windows, and times are seconds that can reach 1,000,000,000.
Write banner_changes(alerts).
alerts[i] is [start, end, score]. The alert is open from start up to, but not including, end, and start < end.[moment, level]: from this moment the banner shows level.Input: alerts = [[1, 6, 4], [2, 4, 9], [3, 8, 6], [10, 12, 5]]
Output: [[1, 4], [2, 9], [4, 6], [8, 0], [10, 5], [12, 0]]Only the moments where an alert opens or closes can change the banner: 1, 2, 3, 4, 6, 8, 10 and 12. They make 7 gaps, and in each gap the banner shows one level.
| Gap | Alerts open in it | Banner level | Listed? |
|---|---|---|---|
| 1 to 2 | the first (4) | 4 | yes, it differs from 0 |
| 2 to 3 | the first (4), the second (9) | 9 | yes |
| 3 to 4 | the first, the second, the third (6) | 9 | no, same as before |
| 4 to 6 | the first, the third | 6 | yes, it differs from 9 |
| 6 to 8 | the third | 6 | no |
| 8 to 10 | none | 0 | yes |
| 10 to 12 | the fourth (5) | 5 | yes |
After 12 nothing is open, so the last change is [12, 0].
Input: alerts = [[1, 5, 3], [5, 9, 3]]
Output: [[1, 3], [9, 0]]The first alert closes at 5 as the second opens, and both score 3. The banner never changes at 5.
Input: alerts = [[1, 3, 2], [6, 8, 2]]
Output: [[1, 2], [3, 0], [6, 2], [8, 0]]Between 3 and 6 nothing is open, so the banner shows 0 and comes back at 6.
1 ≤ len(alerts) ≤ 10,0001 ≤ start < end ≤ 1,000,000,000 and 1 ≤ score ≤ 1,000,000,000At most 20,000 moments matter, because each alert adds an opening and a closing. Checking every alert at every moment costs 20,000 × 10,000 = 200 million checks.
A table with one slot per second is impossible, since times reach a billion. But only the order of the 20,000 moments matters, so a tree over the gaps between them has at most 20,000 leaves.