-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathValidMountainArray_941.java
106 lines (94 loc) · 3.42 KB
/
ValidMountainArray_941.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.leetcode.problems;
import static com.util.LogUtil.getArrayAsString;
import static com.util.LogUtil.logIt;
/**
* @author neeraj on 2019-08-04
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class ValidMountainArray_941 {
public static void main(String[] args) {
isValidMountainArray(new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1});
isValidMountainArray(new int[]{1, 2});
isValidMountainArray(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9});
isValidMountainArray(new int[]{1, 2, 3, 4, 50, 4, 3, 2, 1});
}
public static void isValidMountainArray(int[] arr) {
logIt("Is Valid Mountain Array " + getArrayAsString(arr) + " ? " + validMountainArrayEasyApproach(arr));
}
/**
* Using 2 Pointer Approach
*
* @param sequence
* @return
*/
public static Boolean validMountainArrayEasyApproach(int[] sequence) {
if (sequence == null || sequence.length < 3) {
return false;
}
int i = 0;
// Step 1) Find Increasing Sequence
while (i < sequence.length - 1 && sequence[i + 1] > sequence[i]) {
i++;
}
// After Step 1) our i will be at the element before which all the elements are smaller.
// But we need to handle edge case
// what is Sequence is strictly increasing/decreasing
if (i == 0 || i + 1 >= sequence.length) {
return false;
}
// Find Strictly decreasing sequence
while (i < sequence.length - 1 && sequence[i + 1] < sequence[i]) {
i++;
}
// If by this time we didn't reach the end of array that means this is not a valid mountain.
if (sequence.length - i <= 1) {
return true;
} else {
return false;
}
}
public static Boolean validMountainArray(int[] A) {
if (A == null || A.length < 3) {
return false;
}
Boolean uptrend = true;
Boolean isPeakValueAvailable = false;
int counter = 1;
int previousVal = A[0];
while (counter < A.length) {
if (uptrend) {
if (A[counter] > previousVal) {
isPeakValueAvailable = true;
previousVal = A[counter];
counter++;
} else {
// If the currentValue is less than previousVal, but
// Since if it's strictly decreasing sequence, so there might no peakValue
// found till now which means [0,1,....i-1] < Peak(ith Index)
// Hence in that situation we just return false.
if (!isPeakValueAvailable) {
return false;
} else {
// We found a peak value, so it's time find the downtrend
uptrend = false;
}
}
} else { // Now values are decreasing after reaching the peak.
if (A[counter] < previousVal) {
previousVal = A[counter];
counter++;
} else {
return false;
}
}
}
// If after whole traversal, we are still in uptrend, that means
// the array is in strictly increasing shape.
if (uptrend) {
return false;
} else {
return true;
}
}
}