mediumArraysPattern: Array traversal and pattern recognition
Peak Temperature Indices Solution
Problem Statement
Given a sequence of temperature values stored in the array `temperatures`, find the indices of all temperature readings that exceed their immediate neighbors. If no such readings exist, return an empty list.
Examples
Example 1:
Input:[1, 2, 3, 2, 1]
Output:[2]
Explanation: The temperature at index 2 is higher than its adjacent readings.
Example 2:
Input:[1, 1, 1, 1, 1]
Output:[]
Explanation: There are no peak temperature readings.
Constraints
- 1 <= array length <= 1000
- -10000 <= temperature reading <= 10000
Time: O(n) Space: O(1)
The optimal approach involves iterating through the array and checking each element with its adjacent elements, resulting in a linear time complexity of O(n). This approach is more efficient and suitable for large arrays.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
