A peak in an array arr is an element that is greater than its previous and next element in arr.
You are given an integer array nums and a 2D integer array queries.
You have to process queries of two types:
queries[i] = [1, li, ri], determine the count of peak elements in the subarray nums[li..ri].queries[i] = [2, indexi, vali], change nums[indexi] to vali.Return an array answer containing the results of the queries of the first type in order.
Notes:
Example 1:
Input: nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]
Output: [0]
Explanation:
First query: We change nums[3] to 4 and nums becomes [3,1,4,4,5].
Second query: The number of peaks in the [3,1,4,4,5] is 0.
Example 2:
Input: nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]
Output: [0,1]
Explanation:
First query: nums[2] should become 4, but it is already set to 4.
Second query: The number of peaks in the [4,1,4] is 0.
Third query: The second 4 is a peak in the [4,1,4,2,1].
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 1051 <= queries.length <= 105queries[i][0] == 1 or queries[i][0] == 2i that:
queries[i][0] == 1: 0 <= queries[i][1] <= queries[i][2] <= nums.length - 1queries[i][0] == 2: 0 <= queries[i][1] <= nums.length - 1, 1 <= queries[i][2] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
One requested span is scanned directly. The walk starts just after the left bound and stops just before the right bound, because those two positions cannot be peaks. Each interior value is counted when it is strictly greater than the neighbor on each side.
Here's how the algorithm would work step-by-step:
def count_peaks_in_subarray_brute_force(numbers, start_index, end_index):
peak_count = 0
# Iterate through the subarray, excluding the first and last elements since they cannot be peaks.
for current_index in range(start_index + 1, end_index):
number_to_check = numbers[current_index]
left_neighbor = numbers[current_index - 1]
right_neighbor = numbers[current_index + 1]
# A peak must be strictly greater than both of its immediate neighbors.
is_peak = number_to_check > left_neighbor and number_to_check > right_neighbor
# We only count the element if it meets the definition of a peak.
if is_peak:
peak_count += 1
return peak_countA binary indexed tree stores a one at every index that is currently a peak and a zero everywhere else. Building it marks the initial peaks, a count reads the sum of marks strictly inside the requested span, and an update recomputes the peak status of the changed index and its two neighbors. Only a mark that flips is added or removed in the tree.
Here's how the algorithm would work step-by-step:
class FenwickTree:
def __init__(self, size):
self.tree_sums = [0] * (size + 1)
def update(self, index, delta):
index += 1
while index < len(self.tree_sums):
self.tree_sums[index] += delta
index += index & (-index)
def query(self, index):
index += 1
current_sum = 0
while index > 0:
current_sum += self.tree_sums[index]
index -= index & (-index)
return current_sum
def query_range(self, start_index, end_index):
if start_index > end_index:
return 0
end_sum = self.query(end_index)
start_sum = self.query(start_index - 1)
return end_sum - start_sum
class PeaksInArraySolver:
def __init__(self, numbers):
self.numbers = numbers
self.number_of_elements = len(numbers)
self.peak_markers = [0] * self.number_of_elements
self.fenwick_tree = FenwickTree(self.number_of_elements)
self._initialize_peaks()
def _is_peak(self, index):
if 1 <= index < self.number_of_elements - 1:
return self.numbers[index] > self.numbers[index - 1] and self.numbers[index] > self.numbers[index + 1]
return False
def _initialize_peaks(self):
# Pre-calculating all initial peaks ensures our data structure is ready for fast queries.
for index in range(1, self.number_of_elements - 1):
if self._is_peak(index):
self.peak_markers[index] = 1
self.fenwick_tree.update(index, 1)
def count_peaks_in_range(self, start_index, end_index):
# Querying the tree is much faster than re-scanning the array for every request.
if start_index + 1 > end_index - 1:
return 0
return self.fenwick_tree.query_range(start_index + 1, end_index - 1)
def update_value(self, index, new_value):
affected_indices = {index - 1, index, index + 1}
old_peak_states = {}
for affected_index in affected_indices:
if 0 <= affected_index < self.number_of_elements:
old_peak_states[affected_index] = self._is_peak(affected_index)
self.numbers[index] = new_value
# Updating the Fenwick tree only for changed peak statuses avoids redundant calculations.
for affected_index in affected_indices:
if 0 <= affected_index < self.number_of_elements:
new_peak_state = self._is_peak(affected_index)
old_peak_state = old_peak_states.get(affected_index, False)
if new_peak_state != old_peak_state:
# The delta is +1 for a new peak, -1 for a removed peak.
delta = 1 if new_peak_state else -1
self.peak_markers[affected_index] = 1 if new_peak_state else 0
self.fenwick_tree.update(affected_index, delta)| Case | How to Handle |
|---|---|
| The queried span has fewer than three positions | There is no interior index, so the count is zero. |
| A value only ties one of its neighbors | It is not a peak and does not add to the count. |
| An update creates a new peak | The stored mark for that index increases by one before later counts. |
| An update removes a peak | The stored mark decreases by one so the index is left out of later counts. |
| An endpoint of the whole array changes | That endpoint stays ineligible as a peak, while its inner neighbor may gain or lose peak status. |
| A count covers the entire array | Every current interior peak is included, and the two ends are left out. |