comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
中等 |
|
给你一个整数数组 nums
和两个整数:left
及 right
。找出 nums
中连续、非空且其中最大元素在范围 [left, right]
内的子数组,并返回满足条件的子数组的个数。
生成的测试用例保证结果符合 32-bit 整数范围。
示例 1:
输入:nums = [2,1,4,3], left = 2, right = 3 输出:3 解释:满足条件的三个子数组:[2], [2, 1], [3]
示例 2:
输入:nums = [2,9,2,5,6], left = 2, right = 8 输出:7
提示:
1 <= nums.length <= 105
0 <= nums[i] <= 109
0 <= left <= right <= 109
题目要我们统计数组 nums
中,最大值在区间
对于区间
对于本题,我们设计一个函数 nums
中,最大值不超过
- 用变量
$cnt$ 记录最大值不超过$x$ 的子数组的个数,用$t$ 记录当前子数组的长度。 - 遍历数组
nums
,对于每个元素$nums[i]$ ,如果$nums[i] \leq x$ ,则当前子数组的长度加一,即$t=t+1$ ,否则当前子数组的长度重置为 0,即$t=0$ 。然后将当前子数组的长度加到$cnt$ 中,即$cnt = cnt + t$ 。 - 遍历结束,将
$cnt$ 返回即可。
时间复杂度 nums
的长度。
class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
def f(x):
cnt = t = 0
for v in nums:
t = 0 if v > x else t + 1
cnt += t
return cnt
return f(right) - f(left - 1)
class Solution {
public int numSubarrayBoundedMax(int[] nums, int left, int right) {
return f(nums, right) - f(nums, left - 1);
}
private int f(int[] nums, int x) {
int cnt = 0, t = 0;
for (int v : nums) {
t = v > x ? 0 : t + 1;
cnt += t;
}
return cnt;
}
}
class Solution {
public:
int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {
auto f = [&](int x) {
int cnt = 0, t = 0;
for (int& v : nums) {
t = v > x ? 0 : t + 1;
cnt += t;
}
return cnt;
};
return f(right) - f(left - 1);
}
};
func numSubarrayBoundedMax(nums []int, left int, right int) int {
f := func(x int) (cnt int) {
t := 0
for _, v := range nums {
t++
if v > x {
t = 0
}
cnt += t
}
return
}
return f(right) - f(left-1)
}
我们还可以枚举数组中每个元素
我们可以使用单调栈方便地求出
时间复杂度
相似题目:
class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
n = len(nums)
l, r = [-1] * n, [n] * n
stk = []
for i, v in enumerate(nums):
while stk and nums[stk[-1]] <= v:
stk.pop()
if stk:
l[i] = stk[-1]
stk.append(i)
stk = []
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] < nums[i]:
stk.pop()
if stk:
r[i] = stk[-1]
stk.append(i)
return sum(
(i - l[i]) * (r[i] - i) for i, v in enumerate(nums) if left <= v <= right
)
class Solution {
public int numSubarrayBoundedMax(int[] nums, int left, int right) {
int n = nums.length;
int[] l = new int[n];
int[] r = new int[n];
Arrays.fill(l, -1);
Arrays.fill(r, n);
Deque<Integer> stk = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
int v = nums[i];
while (!stk.isEmpty() && nums[stk.peek()] <= v) {
stk.pop();
}
if (!stk.isEmpty()) {
l[i] = stk.peek();
}
stk.push(i);
}
stk.clear();
for (int i = n - 1; i >= 0; --i) {
int v = nums[i];
while (!stk.isEmpty() && nums[stk.peek()] < v) {
stk.pop();
}
if (!stk.isEmpty()) {
r[i] = stk.peek();
}
stk.push(i);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (left <= nums[i] && nums[i] <= right) {
ans += (i - l[i]) * (r[i] - i);
}
}
return ans;
}
}
class Solution {
public:
int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {
int n = nums.size();
vector<int> l(n, -1);
vector<int> r(n, n);
stack<int> stk;
for (int i = 0; i < n; ++i) {
int v = nums[i];
while (!stk.empty() && nums[stk.top()] <= v) stk.pop();
if (!stk.empty()) l[i] = stk.top();
stk.push(i);
}
stk = stack<int>();
for (int i = n - 1; ~i; --i) {
int v = nums[i];
while (!stk.empty() && nums[stk.top()] < v) stk.pop();
if (!stk.empty()) r[i] = stk.top();
stk.push(i);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (left <= nums[i] && nums[i] <= right) {
ans += (i - l[i]) * (r[i] - i);
}
}
return ans;
}
};
func numSubarrayBoundedMax(nums []int, left int, right int) (ans int) {
n := len(nums)
l := make([]int, n)
r := make([]int, n)
for i := range l {
l[i], r[i] = -1, n
}
stk := []int{}
for i, v := range nums {
for len(stk) > 0 && nums[stk[len(stk)-1]] <= v {
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
l[i] = stk[len(stk)-1]
}
stk = append(stk, i)
}
stk = []int{}
for i := n - 1; i >= 0; i-- {
v := nums[i]
for len(stk) > 0 && nums[stk[len(stk)-1]] < v {
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
r[i] = stk[len(stk)-1]
}
stk = append(stk, i)
}
for i, v := range nums {
if left <= v && v <= right {
ans += (i - l[i]) * (r[i] - i)
}
}
return
}