Leetcode 2951: Find the Peaks

grid47
grid47
Exploring patterns and algorithms
Jan 16, 2024 4 min read

You are given a list called mountain, which contains integers. Your task is to find all the indices that represent peaks in the list. A peak is defined as an element that is strictly greater than its immediate left and right neighbors. The first and last elements of the list cannot be peaks. Return a list of indices that correspond to the peaks in the mountain array.
Problem
Approach
Steps
Complexity
Input: A 0-indexed list of integers, `mountain`, representing the array.
Example: mountain = [3, 2, 3, 4, 1]
Constraints:
• 3 <= mountain.length <= 100
• 1 <= mountain[i] <= 100
Output: Return a list of indices corresponding to the peaks in the array.
Example: [2]
Constraints:
Goal: Identify indices where the corresponding value is greater than its neighbors.
Steps:
• Loop through the list from index 1 to n-2.
• For each index, check if the value is greater than the previous and next values.
• If true, add the index to the result list.
Goal: The length of the list should be between 3 and 100, and each element should be between 1 and 100.
Steps:
• 3 <= mountain.length <= 100
• 1 <= mountain[i] <= 100
Assumptions:
• The input list will always contain at least 3 elements.
Input: Input: mountain = [3, 2, 3, 4, 1]
Explanation: The peak is at index 2 because the value at index 2 (3) is greater than both its neighbors (2 and 4).

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus