Problem 393972 · easy · Level 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 = 6, adjustments = [[2, 4, 3], [0, 2, -2], [5, 5, 4]]
Output: [-2, -2, 1, 3, 3, 4]
Explanation: after the first adjustment the zones read [0, 0, 3, 3, 3, 0]; the second gives [-2, -2, 1, 3, 3, 0]; the third adds 4 to zone 5.

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…