-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPeakElement.java
More file actions
76 lines (67 loc) · 1.51 KB
/
FindPeakElement.java
File metadata and controls
76 lines (67 loc) · 1.51 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package codingInterview;
public class FindPeakElement {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 4, 1, 5, 5, 1 };
// int index = findPeak1(a);
// System.out.println(index + ", " + a[index]);
int index = findPeak2(a);
System.out.println(index + ", " + a[index]);
}
private static int findPeak2(int[] a) {
// TODO Auto-generated method stub
if (a == null || a.length == 0) {
return -1;
}
int index = 0;
int max = a[0];
for (int i = 1; i < a.length - 1; i++) {
if (a[i] > a[i - 1] && a[i] > a[i + 1] && a[i] > max) {
index = i;
max = a[i];
}
}
if (a[a.length - 1] > max) {
index = a.length - 1;
max = a[a.length - 1];
}
return index;
}
/**
* peak is the element larger than its neighbors
* suppose a[-1],a[n] to be Integer.minvalue
*
* @param a
* @return
*/
private static int findPeak1(int[] a) {
// TODO Auto-generated method stub
if (a == null || a.length == 0) {
return -1;
}
int length = a.length;
int index = -1;
int max = Integer.MIN_VALUE;
if (length == 1)
return 0;
for (int i = 0; i < length; i++) {
if (i != 0 && i != length - 1) {
if (a[i] > a[i - 1] && a[i] > a[i + 1] && a[i] > max) {
index = i;
max = a[i];
}
} else if (i == 0) {
if (a[i] > a[i + 1] && a[i] > max) {
index = i;
max = a[i];
}
} else if (i == length - 1) {
if (a[i] > a[i - 1] && a[i] > max) {
index = max;
max = a[i];
}
}
}
return index;
}
}