【LeetCode刷题(中等程度)】795. 区间子数组个数(前缀和题目)

给定一个元素都是正整数的数组A ,正整数 L 以及 R (L <= R)。

求连续、非空且其中最大元素满足大于等于L 小于等于R的子数组个数。

例如 :
输入:
A = [2, 1, 4, 3]
L = 2
R = 3
输出: 3
解释: 满足条件的子数组: [2], [2, 1], [3].
注意:

L, R 和 A[i] 都是整数,范围在 [0, 10^9]。
数组 A 的长度范围在[1, 50000]。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-subarrays-with-bounded-maximum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:前缀和。具体看这位大佬的评论区前缀和详解

class Solution {
public:
    int numSubarrayBoundedMax(vector& A, int L, int R) {
        return notGreator(A,R) - notGreator(A,L-1);
    }

    int notGreator(vector&A,int R)
    {
        int ans = 0;
        int cnt = 0;

        for(int i = 0;i < A.size();++i)
        {
            if(A[i] <=R)
            {
                cnt +=1;
            }
            else
            {
                cnt = 0;
            }
            ans +=cnt;
        }
        return ans;
    }
};

你可能感兴趣的:(LeetCode刷题)