-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path845.java
63 lines (59 loc) · 1.8 KB
/
845.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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int longestMountain(int[] A) {
int n;
if (A == null || (n = A.length) < 3) {
return 0;
}
int i = 1, max = 0;
while (i < n) {
// find the start of the up left mountain foot
while (i < n && A[i] <= A[i - 1]) {
i++;
}
int start = i - 1;
while (i < n && A[i] > A[i - 1]) {
i++;
}
if (i >= n) {
return max;
}
if (A[i] == A[i - 1]) {
continue;
}
while (i < n && A[i] < A[i - 1]) {
i++;
}
int end = i - 1;
max = Math.max(max, end - start + 1);
}
return max;
}
}
__________________________________________________________________________________________________
sample 38832 kb submission
class Solution {
public int longestMountain(int[] A) {
int N = A.length;
int count = 0;
int j = 0;
while(j < N){
int end = j;
if(end+1 < N && A[end] < A[end+1]){
while(end+1 < N && A[end] < A[end+1]){
end++;
}
if(end+1 < N && A[end] > A[end+1]){
while(end + 1 < N && A[end] > A[end+1]){
end++;
}
count = Math.max(count, end-j+1);
}
}
j = Math.max(end, j+1);
}
return count;
}
}
__________________________________________________________________________________________________