You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.
Return an array that consists of indices of peaks in the given array in any order.
Notes:
Example 1:
Input: mountain = [2,4,4] Output: [] Explanation: mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array. mountain[1] also can not be a peak because it is not strictly greater than mountain[2]. So the answer is [].
Example 2:
Input: mountain = [1,4,3,8,5] Output: [1,3] Explanation: mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array. mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1]. But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3].
Constraints:
3 <= mountain.length <= 1001 <= mountain[i] <= 100This approach involves iterating through the given array and checking each element to find the peaks. A peak is defined as an element that is strictly greater than its neighboring elements. Specifically, an element at index i is a peak if mountain[i] > mountain[i-1] and mountain[i] > mountain[i+1]. We must ensure that we do not check the first or last elements, as they cannot be peaks. The time complexity of this approach is O(n), where n is the number of elements in the mountain array, because we make one pass through the array. The space complexity is O(1), as we use a constant amount of extra memory.
The solution involves iterating through each element from the second to the second last element of the array. We check if the current element is strictly greater than its neighboring elements. If it is, we store the index in a dynamically allocated array and increase the return size. Finally, we return this array of indices.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n) for the dynamic array to store the peaks.
This approach aims to optimize by reducing redundant checks and utilizing a single iteration efficiently. Given the constraints, the optimized solution also iterates through the array but takes extra care to minimize checks by assuming certain boundaries have already been met. The time complexity is similar to the brute force approach at O(n), but we attempt to improve constant factors. The space complexity remains O(1) for constant auxiliary space, but realistic implementations use O(n) for output storage.
This single-pass, optimized solution follows a similar logic to the brute force method but with minor optimizations that reduce overhead from conditional checks without altering the O(n) complexity and assuming redundancy might help in micro-optimizations in alternative settings. The approach scans and efficiently stores indices of peaks using minimal logic checks.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n) for the storage of results.
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n) |
| Optimized Single Pass Approach | Time Complexity: O(n) |
BS-9. Find Peak Element • take U forward • 261,196 views views
Watch 9 more video solutions →Practice Find the Peaks with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor