-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path162. Find Peak Element
More file actions
44 lines (34 loc) · 872 Bytes
/
162. Find Peak Element
File metadata and controls
44 lines (34 loc) · 872 Bytes
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
class Solution {
public int findPeakElement(int[] nums) {
/*
int index = 0, max = nums[0];
for(int i = 0; i < nums.length; i++)
{
if(max < nums[i])
{
max = nums[i];
index = i;
}
}
return index;
*/
return findPeakElementBinarySearch(nums);
}
int findPeakElementBinarySearch(int[] nums)
{
int start = 0, end = nums.length - 1;
while(start < end)
{
int mid = start + (end - start) / 2;
if(nums[mid] < nums[mid + 1])
{
start = mid + 1;
}
else
{
end = mid;
}
}
return start;
}
}