package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev
# README
3187. Peaks in Array
Solution idea
Segment Tree (sum)
- segment tree (sum) not on
nums[]
, but onpeak[]
pushDown()
not necessary for queryRange() bc no updateRange()- when
queryRange(l, r)
: ifl==r
, no peak for single element - when
queryRange(l, r)
: ifl<r
, don't count peaks on both ends ofsubarray[l:r]
i.e.peak[l]
andpeak[r]
e.g.[0 0 1] => 0 peaks
- because we can't count peaks on both ends of
subarray[l:r]
, we need to keep updatingpeak[]
, meaning only updatingnums[]
is not enough - every time
nums[i]
is updated, need to check 3 indices:i-1
,i
,i+1
, whether their corresponding numbers create new peaks or slience peaks, respectively.
Time complexity = $O(Q*\log n)$ where $Q = $ number of queries, $n = $ length of nums[]