Ctrl K

Peaks Under Updates

Hard
You are given an array of heights heights and a list of requests requests. A peak of a segment is an element that is strictly greater than both of its neighbours inside that segment. The first and last elements of a segment can never be peaks. There are two kinds of requests: - [1, l, r]: count the peaks in the segment heights[l..r] (inclusive); - [2, i, v]: set heights[i] to v. Process the requests in order and return the list of answers to the requests of the first kind.
Example 1
heights=[2, 5, 3, 6, 1], requests=[[1, 0, 4], [2, 2, 7], [1, 0, 4]] → [2, 1]

At first the peaks are 5 and 6, so the answer is 2. Then the third height becomes 7: the array is [2, 5, 7, 6, 1] and the only peak is 7.

Example 2
heights=[1, 4, 2, 4, 1], requests=[[1, 1, 3], [1, 0, 2]] → [0, 1]

In the segment [4, 2, 4] the ends cannot be peaks and 2 is lower than its neighbours, so the answer is 0. In [1, 4, 2] the 4 is a peak, so the answer is 1.

Example 3
heights=[3, 3, 3], requests=[[2, 1, 4], [1, 0, 2]] → [1]
Constraints
  • 3 ≤ len(heights) ≤ 10⁵
  • 1 ≤ heights[i] ≤ 10⁵
  • 1 ≤ len(requests) ≤ 10⁵
  • For [1, l, r]: 0 ≤ l ≤ r ≤ len(heights) − 1
  • For [2, i, v]: 0 ≤ i ≤ len(heights) − 1, 1 ≤ v ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCity Block Outline HardSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardA Dark Gap Between Street Lamps Hard
loading Python…
Input
heights=[2, 5, 3, 6, 1], requests=[[1, 0, 4], [2, 2, 7], [1, 0, 4]]
Expected
[2, 1]

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.