Peaks in Array II Problem
Description
LeetCode Problem 4017.
You are given an integer array nums of length n and a 2D integer array queries.
A subarray nums[i..j] is called a peak subarray if:
- Its length is at least 3.
- There exists an index
ksuch thati < k < jand:nums[k] > nums[k - 1]nums[k] > nums[k + 1]
You have to process queries of two types:
[1, l_i, r_i]: Calculate the number of peak subarrays fully contained withinnums[l_i..r_i].[2, index_i, val_i]: Updatenums[index_i]toval_i. This update applies to all subsequent queries.
Return an array answer, where answer[i] is the answer to the i^th query of type 1 in the order they appear.
Example 1:
Input: nums = [1,3,2,4], queries = [[1,0,3],[2,1,1],[1,0,3]]
Output: [2,0]
Explanation:
- Query [1, 0, 3]:
- [1, 3, 2]: choose k = 1. Then nums[k] = 3, nums[k - 1] = 1, and nums[k + 1] = 2. Since 3 > 1 and 3 > 2, this is a peak subarray.
- [1, 3, 2, 4]: choose k = 1. Then nums[k] = 3, nums[k - 1] = 1, and nums[k + 1] = 2. Since 3 > 1 and 3 > 2, this is a peak subarray.
- Query [2, 1, 1]: Update nums[1] to 1. The array becomes [1, 1, 2, 4].
- Query [1, 0, 3]: There are no peak subarrays now.
- Thus, answer = [2, 0].
Example 2:
Input: nums = [9,8,9,8], queries = [[1,1,3],[2,2,1],[1,0,2]]
Output: [1,0]
Explanation:
- Query [1, 1, 3]:
- nums[1..3] = [8, 9, 8]: choose k = 2. Then nums[k] = 9, nums[k - 1] = 8, and nums[k + 1] = 8. Since 9 > 8 and 9 > 8, this is a peak subarray.
- Query [2, 2, 1]: Update nums[2] to 1. The array becomes [9, 8, 1, 8].
- Query [1, 0, 2]: There are no peak subarrays.
- Thus, answer = [1, 0].
Example 3:
Input: nums = [3,6,2,7,1], queries = [[1,1,3],[2,3,0],[1,0,4]]
Output: [0,3]
Explanation:
- Query [1, 1, 3]: The only subarray of length at least 3 is [6, 2, 7]. Its only possible peak index is k = 2, but nums[2] = 2 is less than both nums[1] = 6 and nums[3] = 7, so it is not a peak subarray.
- Query [2, 3, 0]: Update nums[3] to 0. The array becomes [3, 6, 2, 0, 1].
- Query [1, 0, 4]:
- [3, 6, 2]: choose k = 1. Then nums[k] = 6, nums[k - 1] = 3, and nums[k + 1] = 2. Since 6 > 3 and 6 > 2, this is a peak subarray.
- [3, 6, 2, 0]: choose k = 1. Then nums[k] = 6, nums[k - 1] = 3, and nums[k + 1] = 2. Since 6 > 3 and 6 > 2, this is a peak subarray.
- [3, 6, 2, 0, 1]: choose k = 1. Then nums[k] = 6, nums[k - 1] = 3, and nums[k + 1] = 2. Since 6 > 3 and 6 > 2, this is a peak subarray.
- Thus, answer = [0, 3].
Constraints:
3 <= n == nums.length <= 10^50 <= nums[i] <= 10^51 <= queries.length <= 10^5queries[i] = [1, l_i, r_i]orqueries[i] = [2, index_i, val_i]0 <= l_i < r_i <= n - 10 <= index_i <= n - 10 <= val_i <= 10^5
Solution
Approach: Segment Tree
A subarray nums[i..j] is a peak subarray exactly when some index k with i < k < j is a peak (nums[k] > nums[k - 1] and nums[k] > nums[k + 1]). For a query range [l, r] of length L, there are (L - 1)(L - 2) / 2 subarrays of length at least 3. Count the bad ones instead, those whose inner positions [i + 1, j - 1] hold no peak.
Those inner intervals lie inside maximal runs of non-peak positions among l + 1 .. r - 1, and a run of length m holds m(m + 1) / 2 intervals. So the answer is (L - 1)(L - 2) / 2 minus the sum of m(m + 1) / 2 over those runs.
Keep a segment tree over positions where every node stores its length, the non-peak run at its left end, the non-peak run at its right end, and the sum of m(m + 1) / 2 over its runs. Merging two nodes joins the right run of the left child with the left run of the right child. Setting nums[index] = val can only change whether index - 1, index and index + 1 are peaks, so refresh those three leaves.
Complexity: time O((n + q) log n), space O(n), where q is the number of queries.
class Solution:
def countOfPeaks(self, nums: List[int], queries: List[List[int]]) -> List[int]:
n = len(nums)
def peak(k: int) -> bool:
return 0 < k < n - 1 and nums[k] > nums[k - 1] and nums[k] > nums[k + 1]
def f(m: int) -> int:
return m * (m + 1) // 2
# a segment tree node is (length, prefix run of non-peaks, suffix run of non-peaks, sum of f(run) over runs)
def merge(a, b):
la, pa, sa, ta = a
lb, pb, sb, tb = b
pre = pa if pa < la else la + pb
suf = sb if sb < lb else lb + sa
return (la + lb, pre, suf, ta + tb - f(sa) - f(pb) + f(sa + pb))
def leaf(k: int):
return (1, 0, 0, 0) if peak(k) else (1, 1, 1, 1)
size = 1
while size < n:
size *= 2
empty = (0, 0, 0, 0)
tree = [empty] * (2 * size)
for k in range(n):
tree[size + k] = leaf(k)
for i in range(size - 1, 0, -1):
tree[i] = merge(tree[2 * i], tree[2 * i + 1])
def query(lo: int, hi: int) -> int:
# sum of f(run) over the maximal non-peak runs inside positions [lo, hi]
left, right = empty, empty
lo += size
hi += size + 1
while lo < hi:
if lo & 1:
left = merge(left, tree[lo])
lo += 1
if hi & 1:
hi -= 1
right = merge(tree[hi], right)
lo >>= 1
hi >>= 1
return merge(left, right)[3]
ans = []
for t, x, y in queries:
if t == 1:
length = y - x + 1
total = (length - 1) * (length - 2) // 2
bad = query(x + 1, y - 1) if length > 2 else 0
ans.append(total - bad)
else:
nums[x] = y
for k in (x - 1, x, x + 1):
if 0 <= k < n:
i = size + k
tree[i] = leaf(k)
i >>= 1
while i:
tree[i] = merge(tree[2 * i], tree[2 * i + 1])
i >>= 1
return ans