[코테 풀이] Find the Peaks

시내·2024년 6월 29일

Q_2951) Find the Peaks

출처 : https://leetcode.com/problems/find-the-peaks/

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:

  • A peak is defined as an element that is strictly greater than its neighboring elements.

  • The first and last elements of the array are not a peak.

class Solution {
    public List<Integer> findPeaks(int[] mountain) {
        List<Integer> answer = new ArrayList<>();
        for (int i = 1; i < mountain.length - 1; i++) {
            if ((mountain[i] > mountain[i - 1]) && (mountain[i] > mountain[i + 1])) {
                answer.add(i);
            }
        }
        return answer;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글