LeetCode795 Count the Number of Subarrays that max value is Within Range of Given[L, R](NFU)

this problem is actually a lot like LC907 Sum of subarray minimums, which requires us to calculate the sum of all the minimum element of all subarray of given array.
now, current problem requires us to return the number of subarray that the value of maximum of this subarray is within the range of given [L, R]

so think about how we solve the LC907 problem, we use two monotonic increasing stacks. for every fixed value, we calculate the distance of its ple and nle. and for our current problem, we need to make sure that the fixed value is within the range of [L, R];
so based on that idea, I come up with my solution based on LC907
However, it is not accepted.

class Solution {
    public int numSubarrayBoundedMax(int[] A, int L, int R) {
        Stack<int[]> left = new Stack<>(); //int[0] is the value and int[1] is index
        Stack<int[]> right = new Stack<>();
        
        int[] distLeft = new int[A.length];
        int[] distRight = new int[A.length];
        
        for (int i=0; i<A.length; i++) {
            while (!left.isEmpty() && left.peek()[0] >= A[i]) {
                left.pop();
            }
            distLeft[i] = left.isEmpty() ? i + 1: i-left.peek()[1];
            left.push(new int[]{A[i], i});
        }
        for (int i = A.length - 1; i>=0; i--) {
            while (!right.isEmpty() && right.peek()[0] > A[i]) {
                right.pop();
            }
            distRight[i] = right.isEmpty() ? A.length - i: right.peek()[1] - i;
            right.push(new int[]{A[i], i});
        }
        
        int count = 0;
        for (int i = 0; i<A.length; i++) {
            if (A[i] >= L && A[i] <= R) {
                count += distLeft[i] * distRight[i];
            }
        }
        return count;
    }
}

and the solution provided by leetcode is pretty simple, it is actually a very simple dp problem.
so basically, we define dp[i] as the number of subarrays that ends A[i] and satifiy our boundary requirements.

class Solution {
    public int numSubarrayBoundedMax(int[] A, int L, int R) {
        return count(A, R) - count(A, L-1); //count function can calculate the number of subarrays in A which max is less than given max value
    }

    public int count(int[] A, int bound) {
        int ans = 0;
        int cur = 0; //
        for (int x: A) { //for each element in A
            cur = x <= bound ? cur + 1 : 0; 
            ans += cur;
        }//each for loop, the cur represents for the subarray the ends with x that maximum is within given bound. so cur is actually dp[i], and if x<= bound, so dp[i] = dp[i-1] + 1, but if not, then dp[i]=0, means nothing subarray ends with A[i] are out of bound.
        //more explainaton of dp[i] = dp[i-1] + 1. why? because (every subarray ends with A[i-1])+A[i], so that's totally of dp[i-1], and we have another subarray which only contains A[i] that meet our needs, so the totally number of subarrays that ends wth A[i] is dp[i-1]+1  
        return ans;
    }
}
posted @ 2020-05-17 05:39  EvanMeetTheWorld  阅读(19)  评论(0)    收藏  举报