-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindAPeakElement2.java
78 lines (62 loc) · 2.02 KB
/
FindAPeakElement2.java
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
77
78
/*https://leetcode.com/problems/find-a-peak-element-ii/*/
class Solution {
public int[] findPeakGrid(int[][] mat) {
int low = 0, high = mat[0].length;
while (low <= high)
{
int mid = (low+high)/2;
int maxVal = Integer.MIN_VALUE;
int row = 0, col = 0;
for (int j = mid-1; j <= mid+1; ++j)
{
if (j < 0 || j >= mat[0].length) continue;
for (int i = 0; i < mat.length; ++i)
{
if (maxVal < mat[i][j])
{
maxVal = mat[i][j];
row = i;
col = j;
}
}
}
if (col == mid) return new int[]{row,col};
else if (col == mid-1) high = mid-1;
else low = mid+1;
}
return new int[]{};
}
}
class Solution {
public int getPeakIndex(int[] col)
{
int maxIndex = -1, maxElement = Integer.MIN_VALUE;
for (int i =0; i < col.length; i++)
{
if (maxElement < col[i])
{
maxIndex = i;
maxElement = col[i];
}
}
return maxIndex;
}
public int[] findPeakGrid(int[][] mat) {
int r = mat.length, c = mat[0].length;
int low = 0, high = r-1;
if (low == high)
return new int[]{low, getPeakIndex(mat[0])};
while (low <= high)
{
int mid = (low+high)/2;
int peakIndex = getPeakIndex(mat[mid]);
if (mid < r-1 && mat[mid][peakIndex] < mat[mid+1][peakIndex])
low = mid +1;
else if (mid > 0 && mat[mid][peakIndex] < mat[mid-1][peakIndex])
high = mid -1;
else
return new int[]{mid, peakIndex};
}
return new int[]{-1,-1};
}
}