package leetcode;

/**
 * https://leetcode.com/problems/longest-mountain-in-array/
 */
public class Problem845 {
    public int longestMountain(int[] A) {
        if (A.length == 0) {
            return 0;
        }
        int left = -1;
        int right = -1;
        int i = 0;
        int prev = A[0];
        int result = 0;
        while (i < A.length) {
            if (prev < A[i]) {
                if (left >= 0 && right >= 0) {
                    result = Math.max(result, right - left + 1);
                    left = right;
                    right = -1;
                } else {
                    if (left == -1) {
                        left = i - 1;
                    }
                }
                prev = Math.max(prev, A[i]);
            } else if (prev > A[i]) {
                prev = Math.min(prev, A[i]);
                right = i;
            } else {
                left = -1;
                right = -1;
            }
            i++;
        }
        if (left >= 0 && right >= 0) {
            result = Math.max(result, right - left + 1);
        }
        return result;
    }

    public static void main(String[] args) {
        Problem845 prob = new Problem845();
//        System.out.println(prob.longestMountain(new int[]{2, 1, 4, 7, 3, 2, 5})); // 5
//        System.out.println(prob.longestMountain(new int[]{2, 2, 2})); // 0
//        System.out.println(prob.longestMountain(new int[]{1, 1, 2, 3, 4, 2, 3, 2})); // 5
//        System.out.println(prob.longestMountain(new int[]{2, 1})); // 0
//        System.out.println(prob.longestMountain(new int[]{1, 2})); // 0
//        System.out.println(prob.longestMountain(new int[]{1, 2, 1})); // 3
//        System.out.println(prob.longestMountain(new int[]{1, 2, 2, 1})); // 0
//        System.out.println(prob.longestMountain(new int[]{1, 2, 2, 1, 5, 3, 6, 1, 0})); // 0
        System.out.println(prob.longestMountain(new int[]{7, 4, 8})); // 0
    }
}

@@ -221,25 +221,26 @@
     int 
-lef
+resul
 t = 
--1
+0
 ;%0A      
@@ -245,20 +245,19 @@
     int 
-righ
+lef
 t = -1;%0A
@@ -268,17 +268,23 @@
     int 
-i
+current
  = 0;%0A  
@@ -310,38 +310,72 @@
 0%5D;%0A        
-int result = 0
+boolean up = false;%0A        boolean down = false
 ;%0A        wh
@@ -379,17 +379,23 @@
  while (
-i
+current
  %3C A.len
@@ -426,17 +426,23 @@
 rev %3C A%5B
-i
+current
 %5D) %7B%0A   
@@ -463,34 +463,29 @@
 f (left 
-%3E= 0 && right %3E= 0
+== -1 %7C%7C down
 ) %7B%0A    
@@ -504,51 +504,26 @@
     
-result = Math.max(result, right - left +
+left = current -
  1
-)
 ;%0A  
@@ -540,242 +540,116 @@
     
-    left = right;%0A                    right = -1;%0A                %7D else %7B%0A                    if (left == -1) %7B%0A                        left = i - 1;%0A                    %7D%0A                %7D%0A                prev = Math.max(prev, A%5Bi%5D)
+%7D%0A                prev = Math.max(prev, A%5Bcurrent%5D);%0A                up = true;%0A                down = false
 ;%0A  
@@ -665,24 +665,30 @@
  %7D else if (
+up && 
 prev %3E A%5Bi%5D)
@@ -684,17 +684,23 @@
 rev %3E A%5B
-i
+current
 %5D) %7B%0A   
@@ -740,196 +740,23 @@
 , A%5B
-i%5D);%0A                right = i;%0A            %7D else %7B%0A                left = -1;%0A                right = -1;%0A            %7D%0A            i++;%0A        %7D%0A        if (left %3E= 0 && right %3E= 0) %7B%0A
+current%5D);%0A    
     
@@ -793,12 +793,14 @@
 lt, 
-righ
+curren
 t - 
@@ -818,26 +818,24 @@
         
-%7D%0A
         
 return r
@@ -830,851 +830,231 @@
     
-return result
+down = true
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem845 prob = new Problem845();%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B2, 1, 4, 7, 3, 2, 5%7D)); // 5%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B2, 2, 2%7D)); // 0%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B1, 1, 2, 3, 4, 2, 3, 2%7D)); // 5%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B2, 1%7D)); // 0%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B1, 2%7D)); // 0%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B1, 2, 1%7D)); // 3%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B1, 2, 2, 1%7D)); // 0%0A//        System.out.println(prob.longestMountain(new int%5B%5D%7B1, 2, 2, 1, 5, 3, 6, 1, 0%7D)); // 0%0A        System.out.println(prob.longestMountain(new int%5B%5D%7B7, 4, 8%7D)); // 0
+    %7D else %7B%0A                left = -1;%0A                prev = A%5Bcurrent%5D;%0A                up = false;%0A                down = false;%0A            %7D%0A            current++;%0A        %7D%0A        return result;
 %0A   

Solve problem 845