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;
        boolean found = false;
        for (int i = 0; i < A.length; i++) {
            if (L <= A[i] && A[i] <= R) {
                found = true;
                count++;
                result += count;
            } else if (A[i] > R) {
                count = 0;
            } else {
                count++;
                if (found) {
                    result += count - 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, 1, 2, 1, 1, 1}, 2, 3)); // 12
//        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
    }
}

@@ -222,32 +222,59 @@
  int count = 0;%0A
+        int prevCount = 0;%0A
         boolean 
@@ -288,16 +288,16 @@
  false;%0A
-
         
@@ -422,32 +422,67 @@
        count++;%0A
+                prevCount = count;%0A
                 
@@ -529,24 +529,55 @@
 A%5Bi%5D %3E R) %7B%0A
+                found = false;%0A
             
@@ -700,17 +700,17 @@
  += 
-count - 1
+prevCount
 ;%0A  
@@ -741,32 +741,32 @@
     %7D%0A        %7D%0A
+
         return r
@@ -781,1160 +781,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem795 prob = new Problem795();%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B2, 1, 4, 3%7D, 2, 3)); // 3%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 1, 1%7D, 2, 3)); // 0%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 2, 1%7D, 2, 3)); // 6%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 2, 1%7D, 2, 3)); // 4%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 2, 3, 1%7D, 2, 3)); // 8%0A        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 2, 1, 1%7D, 2, 3)); // 9%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 2, 1, 1, 1%7D, 2, 3)); // 12%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 3, 2, 1, 1%7D, 2, 3)); // 11%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 3, 2, 3, 1%7D, 2, 3)); // 13%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 2, 1, 1, 4, 1, 2, 1%7D, 2, 3)); // 13%0A//        System.out.println(prob.numSubarrayBoundedMax(new int%5B%5D%7B1, 1, 2, 3, 1, 4, 1, 2, 1%7D, 2, 3)); // 15%0A    %7D
 %0A%7D%0A

Solve problem 795