-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSolution845.java
86 lines (76 loc) · 2.14 KB
/
Solution845.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
79
80
81
82
83
84
85
86
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 数组中的最长山脉
* @date: 2019/01/12
*/
public class Solution845 {
public static void main(String[] args) {
int[] A = {2, 1, 4, 7, 3, 2, 5};
System.out.println(new Solution845().longestMountain1(A));
}
/**
* In this problem, we take one forward pass to count up hill length (to every point).
* We take another backward pass to count down hill length (from every point).
* Finally a pass to find max(up[i] + down[i] + 1) where up[i] and down[i] should be positives.
*
* @param A
* @return
*/
public int longestMountain1(int[] A) {
if (null == A || 3 > A.length) {
return 0;
}
int ans = 0;
int[] up = new int[A.length];
int[] down = new int[A.length];
for (int i = A.length - 2; i >= 0; --i) {
if (A[i] > A[i + 1]) {
down[i] = down[i + 1] + 1;
}
}
for (int i = 0; i < A.length; ++i) {
if (0 < i && A[i - 1] < A[i]) {
up[i] = up[i - 1] + 1;
}
if (0 < up[i] && 0 < down[i]) {
int len = up[i] + down[i] + 1;
ans = ans > len ? ans : len;
}
}
return ans;
}
public int longestMountain(int[] A) {
if (null == A || 3 > A.length) {
return 0;
}
int ans = 0;
for (int peak = 1; peak < A.length - 1; ++peak) {
int i = peak;
while (0 < i) {
if (A[i - 1] < A[i]) {
--i;
} else {
break;
}
}
if (peak - i < 1) {
continue;
}
int j = peak;
while (A.length - 1 > j) {
if (A[j] > A[j + 1]) {
++j;
} else {
break;
}
}
if (j - peak < 1) {
continue;
}
int len = j - i + 1;
ans = ans > len ? ans : len;
}
return ans;
}
}