leetcode795+最大值在LR之间的连续的子数组个数,剪枝

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/88047589

https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/

class Solution {
public:
    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
        int res = 0, len = A.size();
        for(int i=0; i<len; i++){
            if(A[i]>R) continue; //类似于剪枝
            int curMax = INT_MIN;
            for(int j=i; j<len; j++){
                curMax = max(curMax, A[j]);
                if(curMax>R) break;//当前以i为起点的子数组都不行,剪枝
                if(curMax>=L) res+=1;
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/88047589