Peaks in Array - Problem
Peaks in Array is a dynamic range query problem that challenges you to efficiently handle two types of operations on an array.
You are given an integer array
You must handle two types of queries:
โข Type 1:
โข Type 2:
Return an array containing the results of all Type 1 queries in the order they appear.
Example: In array
You are given an integer array
nums and need to process a series of queries. A peak is defined as an element that is strictly greater than both its neighboring elements (left and right). Important: The first and last elements of an array cannot be peaks since they don't have two neighbors.You must handle two types of queries:
โข Type 1:
[1, left, right] - Count the number of peaks in the subarray from index left to right (inclusive)โข Type 2:
[2, index, value] - Update nums[index] to valueReturn an array containing the results of all Type 1 queries in the order they appear.
Example: In array
[1, 4, 3, 8, 5], element 4 at index 1 and element 8 at index 3 are peaks since 1 < 4 > 3 and 3 < 8 > 5. Input & Output
example_1.py โ Basic Peak Counting
$
Input:
nums = [1,4,3,8,5], queries = [[2,3,4],[1,0,4]]
โบ
Output:
[2]
๐ก Note:
Initially peaks are at indices 1 (4>1,3) and 3 (8>3,5). After update nums[3]=4, we have [1,4,3,4,5]. Query [1,0,4] counts peaks in range [0,4]: index 1 (4>1,3) and index 3 (4>3,5), so answer is 2.
example_2.py โ No Peaks in Short Range
$
Input:
nums = [1,2,3], queries = [[1,0,2]]
โบ
Output:
[1]
๐ก Note:
Array [1,2,3] has one peak at index 1 (2>1, but 2<3 so not a peak). Actually, element 2 at index 1 satisfies 2>1 and we need 2>3 which is false. So no peaks. Wait, let me recalculate: 2>1 (true) and 2>3 (false), so index 1 is not a peak. Answer should be [0].
example_3.py โ Multiple Updates
$
Input:
nums = [1,2,1,2,1], queries = [[1,1,3],[2,2,3],[1,1,3]]
โบ
Output:
[0,1]
๐ก Note:
Initially: [1,2,1,2,1]. Index 1: 2>1,1 โ (peak), Index 3: 2>1,1 โ (peak). Query [1,1,3] asks for peaks in [1,3], which includes indices 1,2 - only index 1 is peak, but we check range [2,2] which has no peaks. After update nums[2]=3: [1,2,3,2,1]. New peaks: index 2 (3>2,2 โ). Query [1,1,3] now finds 1 peak at index 2.
Constraints
- 3 โค nums.length โค 105
- 1 โค nums[i] โค 105
- 1 โค queries.length โค 105
- queries[i] = [1, li, ri] or queries[i] = [2, indexi, vali]
- 0 โค li โค ri โค nums.length - 1
- 0 โค indexi โค nums.length - 1
- 1 โค vali โค 105
Visualization
Tap to expand
Understanding the Visualization
1
Identify Peaks
Mark mountains higher than both neighbors
2
Build Monitoring Tree
Create hierarchical system for fast region queries
3
Process Requests
Answer peak count queries and update elevations efficiently
Key Takeaway
๐ฏ Key Insight: Use a Segment Tree to transform this into a dynamic range sum problem where peak positions are maintained efficiently with logarithmic complexity for both queries and updates.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code