Approach #1: Math. [Java]
class Solution {
public int maxTurbulenceSize(int[] A) {
int len = A.length;
int inc = 1, dec = 1, result = 1;
for (int i = 1; i < len; ++i) {
if (A[i] < A[i-1]) {
dec = inc + 1;
inc = 1;
} else if (A[i] > A[i-1]) {
inc = dec + 1;
dec = 1;
} else {
inc = 1;
dec = 1;
} result = Math.max(result, Math.max(inc, dec));
} return result;
}
}