|
| 1 | +3187\. Peaks in Array |
| 2 | + |
| 3 | +Hard |
| 4 | + |
| 5 | +A **peak** in an array `arr` is an element that is **greater** than its previous and next element in `arr`. |
| 6 | + |
| 7 | +You are given an integer array `nums` and a 2D integer array `queries`. |
| 8 | + |
| 9 | +You have to process queries of two types: |
| 10 | + |
| 11 | +* <code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of **peak** elements in the subarray <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>. |
| 12 | +* <code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code>val<sub>i</sub></code>. |
| 13 | + |
| 14 | +Return an array `answer` containing the results of the queries of the first type in order. |
| 15 | + |
| 16 | +**Notes:** |
| 17 | + |
| 18 | +* The **first** and the **last** element of an array or a subarray **cannot** be a peak. |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +**Input:** nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]] |
| 23 | + |
| 24 | +**Output:** [0] |
| 25 | + |
| 26 | +**Explanation:** |
| 27 | + |
| 28 | +First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`. |
| 29 | + |
| 30 | +Second query: The number of peaks in the `[3,1,4,4,5]` is 0. |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +**Input:** nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]] |
| 35 | + |
| 36 | +**Output:** [0,1] |
| 37 | + |
| 38 | +**Explanation:** |
| 39 | + |
| 40 | +First query: `nums[2]` should become 4, but it is already set to 4. |
| 41 | + |
| 42 | +Second query: The number of peaks in the `[4,1,4]` is 0. |
| 43 | + |
| 44 | +Third query: The second 4 is a peak in the `[4,1,4,2,1]`. |
| 45 | + |
| 46 | +**Constraints:** |
| 47 | + |
| 48 | +* <code>3 <= nums.length <= 10<sup>5</sup></code> |
| 49 | +* <code>1 <= nums[i] <= 10<sup>5</sup></code> |
| 50 | +* <code>1 <= queries.length <= 10<sup>5</sup></code> |
| 51 | +* `queries[i][0] == 1` or `queries[i][0] == 2` |
| 52 | +* For all `i` that: |
| 53 | + * `queries[i][0] == 1`: `0 <= queries[i][1] <= queries[i][2] <= nums.length - 1` |
| 54 | + * `queries[i][0] == 2`: `0 <= queries[i][1] <= nums.length - 1`, <code>1 <= queries[i][2] <= 10<sup>5</sup></code> |
0 commit comments