package leetcode;

/**
 * https://leetcode.com/problems/longest-mountain-in-array/
 */
public class Problem845 {
    public int longestMountain(int[] A) {
        int left = 0;
        int right = -1;
        int i = 0;
        int prev = Integer.MIN_VALUE;
        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;
                }
                prev = Math.max(prev, A[i]);
            } else if (prev > A[i]) {
                prev = Math.min(prev, A[i]);
                right = i;
            } else {
                // TODO
            }
            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
    }
}

@@ -144,24 +144,85 @@
 (int%5B%5D A) %7B%0A
+        if (A.length == 0) %7B%0A            return 0;%0A        %7D%0A
         int 
@@ -224,25 +224,26 @@
  int left = 
-0
+-1
 ;%0A        in
@@ -298,25 +298,12 @@
 v = 
-Integer.MIN_VALUE
+A%5B0%5D
 ;%0A  
@@ -556,32 +556,155 @@
     right = -1;%0A
+                %7D else %7B%0A                    if (left == -1) %7B%0A                        left = i - 1;%0A                    %7D%0A
                 
@@ -901,15 +901,46 @@
     
-// TODO
+left = -1;%0A                right = -1;
 %0A   
@@ -1460,16 +1460,18 @@
 ); // 5%0A
+//
         
@@ -1526,32 +1526,32 @@
 %5D%7B2, 1%7D)); // 0%0A
-
 //        System
@@ -1685,16 +1685,16 @@
 ); // 3%0A
-
 //      
@@ -1765,16 +1765,186 @@
 ); // 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%0A
     %7D%0A%7D%0A

Update problem 845