Problem 393972 · easy · Phase 03 Linear Management & Searching

Greenhouse Zone Adjustments

difference array · range updates

A greenhouse has n climate zones in a row, all starting at temperature offset 0. A list of adjustments is applied in order: adjustments[i] = [l, r, d] adds d degrees to every zone from l to r inclusive (0-indexed). Return the final offset of every zone.

Examples

Input:  n = 5, adjustments = [[1, 3, 2], [2, 4, -1], [0, 0, 5]]
Output: [5, 2, 1, 1, -1]
Explanation: after the first adjustment the zones read [0, 2, 2, 2, 0]; the second gives [0, 2, 1, 1, -1]; the third adds 5 to zone 0.

Input:  n = 3, adjustments = []
Output: [0, 0, 0]

Constraints

  • 1 <= n <= 10**5, 0 <= len(adjustments) <= 10**5
  • 0 <= l <= r < n, -1000 <= d <= 1000
  • Target complexity: O(n + m) where m is the number of adjustments. Looping over each range is too slow for the largest tests.

Goals

  • Record a range update with two marks instead of a loop
  • Recover final values with one running total
Starting Python…