forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPeakElement.java
More file actions
46 lines (38 loc) · 1.17 KB
/
FindPeakElement.java
File metadata and controls
46 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package binary_search;
/**
* Description: https://leetcode.com/problems/find-peak-element
* Difficulty: Medium
*/
public class FindPeakElement {
/**
* Time complexity: O(log n)
* Space complexity: O(1)
*/
public int findPeakElement(int[] nums) {
if (nums.length == 1) return 0;
if (nums[0] > nums[1]) return 0;
if (nums[nums.length - 1] > nums[nums.length - 2]) return nums.length - 1;
int left = 1;
int right = nums.length - 2;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[mid + 1] && nums[mid] > nums[mid - 1]) return mid;
if (nums[mid] < nums[mid + 1]) {
left = mid + 1; // rising slope, peak on the right
} else {
right = mid - 1; // falling slope, peak on the left
}
}
return -1;
}
/**
* Time complexity: O(n)
* Space complexity: O(1)
*/
public int findPeakElementViaLinearScan(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) return i;
}
return nums.length - 1;
}
}