-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valid Mountain array
39 lines (39 loc) · 998 Bytes
/
Valid Mountain array
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
class Solution {
public boolean validMountainArray(int[] arr) {
if(arr.length<3) return false;
int n=arr.length;
int max=0;
int maxInd=0;
for(int i=0; i<n; i++){
if(arr[i]>max) {
maxInd=i;
max=arr[i];
}
}
if(max==arr[n-1] || max==arr[0]) return false;
int i=maxInd-1, j=maxInd+1;
int maxX=max, maxY=max;
while(i>=0){
if(arr[i]<maxX){
maxX=arr[i];
i--;
// System.out.println(maxX+"maxX");
continue;
}
return false;
}
while(j<n){
if(arr[j]<maxY){
maxY=arr[j];
j++;
// System.out.println(maxY+"maxY");
continue;
}
return false;
}
// System.out.println(i+"i");
// System.out.println(j+"j");
return true;
}
}
#Status: SOlved