“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Looking for peak

A peak element is an element whose value is greater than the value of its left and right neighbors.

Given an input array nums, find the peak element and return its index. The array may contain multiple peaks, in which case you simply return the location of any one of the peaks.

You can assume that NUMs [-1] = nums[n] = -∞.

Examples can be found on the LeetCode website.

Source: LeetCode link: leetcode-cn.com/problems/fi… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

Solution 1: array traversal

First, judge two special cases:

  • If there is only one element, the index bit of the first element is returned.
  • If there are two elements, the corresponding index bit is returned based on the size of the two elements.

If the array nums has more than two elements, then the peak value is traversed from the first element:

  • The first value of the array is evaluated. If the value is greater than the second value, 0 is returned.
  • Otherwise, iterate through the elements from 1 to nums.length-2 to determine whether it is the peak value. If it is the peak value, directly return the corresponding index bit.
  • Finally, if no peak is found, determine whether the last element is greater than the previous element value, if so, return the maximum index bit, otherwise, return -1 to indicate there is no peak.
public class LeetCode_162 {
    public static int findPeakElement(int[] nums) {
        // If there is only one element, return the index bit of the first element
        if (nums.length == 1) {
            return 0;
        }
        // If there are two elements, the corresponding index bit is directly returned according to the size of the two elements
        if (nums.length == 2) {
            if (nums[0] > nums[1]) {
                return 0;
            } else {
                return 1; }}Returns 0 if the first value of the array is greater than the second value
        if (nums[0] > nums[1]) {
            return 0;
        }
        int index = 1;
        // Iterate over elements from 1 to nums.length-2 to determine if it is a peak
        for (; index < nums.length - 1; index++) {
            if (nums[index] > nums[index - 1]) {
                if (nums[index] > nums[index + 1]) {
                    returnindex; }}else {
                if (nums[index] > nums[index + 1]) { index++; }}}If no peak value is found before, determine whether the last element is greater than the previous element value, if so, return the maximum index bit, otherwise, -1 means there is no peak value
        if (nums[nums.length - 1] > nums[nums.length - 2]) {
            return nums.length - 1;
        } else {
            return -1; }}public static void main(String[] args) {
        int[] nums = new int[] {1.2.1.3.5.6.4};
        // Test case, expected return value: 1 or 5System.out.println(findPeakElement(nums)); }}Copy the code

If you want to make the money, you have to take the pain. To be strong in life, you can not care about gains and losses.