package leetcode;

/**
 * https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/
 */
public class Problem795 {
    public int numSubarrayBoundedMax(int[] A, int L, int R) {
        int result = 0;
        int count = 0;
        int max = -1;
        for (int i = 0; i < A.length; i++) {
            max = Math.max(max, A[i]);
            if (L <= max && max <= R) {
                count++;
            } else {
                max = -1;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem795 prob = new Problem795();
        System.out.println(prob.numSubarrayBoundedMax(new int[]{2, 1, 4, 3}, 2, 3)); // 3
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 1, 1, 1}, 2, 3)); // 0
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 1, 2, 1}, 2, 3)); // 6
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 2, 1}, 2, 3)); // 4
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 2, 3, 1}, 2, 3)); // 8
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 1, 2, 1, 1}, 2, 3)); // 9
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 3, 2, 1, 1}, 2, 3)); // 11
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 3, 2, 3, 1}, 2, 3)); // 13
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 1, 2, 1, 1, 4, 1, 2, 1}, 2, 3)); // 13
        System.out.println(prob.numSubarrayBoundedMax(new int[]{1, 1, 2, 3, 1, 4, 1, 2, 1}, 2, 3)); // 15
    }
}

@@ -242,20 +242,29 @@
     
-int max = -1
+boolean found = false
 ;%0A  
@@ -322,69 +322,155 @@
     
-max = Math.max(max, A%5Bi%5D);%0A            if (L %3C= max && max %3C=
+if (L %3C= A%5Bi%5D && A%5Bi%5D %3C= R) %7B%0A                found = true;%0A                count++;%0A                result += count;%0A            %7D else if (A%5Bi%5D %3E
  R) 
@@ -484,34 +484,36 @@
            count
-++
+ = 0
 ;%0A            %7D 
@@ -539,18 +539,105 @@
     
-max =
+count++;%0A                if (found) %7B%0A                    result += count
  -
+ 
 1;%0A
+                %7D%0A
     
@@ -775,16 +775,18 @@
 m795();%0A
+//
         
@@ -867,16 +867,18 @@
 ); // 3%0A
+//
         
@@ -959,16 +959,18 @@
 ); // 0%0A
+//
         
@@ -1051,16 +1051,18 @@
 ); // 6%0A
+//
         
@@ -1140,16 +1140,18 @@
 ); // 4%0A
+//
         
@@ -1325,16 +1325,117 @@
 ); // 9%0A
+//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 2, 1, 1, 1%7D, 2, 3)); // 12%0A//
         
@@ -1520,16 +1520,18 @@
 ; // 11%0A
+//
         
@@ -1608,32 +1608,34 @@
 , 2, 3)); // 13%0A
+//
         System.o
@@ -1724,16 +1724,18 @@
 ; // 13%0A
+//
         

Update problem 795