LeetCode: Find Peak Element 解题报告

Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

SOLUTION 1:

线性查找,时间O(N):

 1 public int findPeakElement1(int[] num) {
 2         if (num == null) {
 3             return 0;
 4         }
 5
 6         if (num.length == 1) {
 7             return 0;
 8         }
 9
10         for (int i = 0; i < num.length; i++) {
11             if (i == 0) {
12                 if (num[i] > num[i + 1]) {
13                     return i;
14                 }
15                 continue;
16             }
17
18             if (i == num.length - 1) {
19                 if (num[i] > num[i - 1]) {
20                     return i;
21                 }
22                 continue;
23             }
24
25             if (num[i] > num[i + 1] && num[i] > num[i - 1]) {
26                 return i;
27             }
28         }
29
30         return -1;
31     }

SOLUTION 2:

使用九章算法的二分法模板,可以达到O(logN)的时间复杂度。原理是:

当找到一个下坡,我们往左移动,当找到一个上坡,我们往右移动,这样我们就可以达到顶峰。

如果找到一个山谷,则向任意方向移动即可。

4

3          3     5

2    2    2

1          1

如上图所示,3,4都是可能的解。

最后循环break时,把l,r的值找一个大的即可。

 1 public int findPeakElement(int[] num) {
 2         if (num == null) {
 3             return 0;
 4         }
 5
 6         if (num.length == 1) {
 7             return 0;
 8         }
 9
10         int l = 0;
11         int r = num.length - 1;
12
13         while (l < r - 1) {
14             int mid = l + (r - l) / 2;
15             if (num[mid] > num[mid + 1] && num[mid] > num[mid - 1]) {
16                 return mid;
17             }
18
19             if (num[mid] > num[mid - 1] && num[mid] < num[mid + 1]) {
20                 // rising area. move right;
21                 l = mid;
22             } else if (num[mid] < num[mid - 1] && num[mid] > num[mid + 1]) {
23                 r = mid;
24             } else {
25                 l = mid;
26             }
27         }
28
29         return num[l] > num[r] ? l: r;
30     }

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/binarySearch/FindPeakElement.java

时间: 2024-10-22 03:12:17

LeetCode: Find Peak Element 解题报告的相关文章

【原创】leetCodeOj --- Find Peak Element 解题报告

题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple pe

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

[LeetCode]Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi