comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
困难 |
|
给定一个整数数组 nums
,返回满足下面条件的 非空、连续 子数组的数目:
- 子数组 是数组的 连续 部分。
- 子数组最左边的元素不大于子数组中的其他元素 。
示例 1:
输入:nums = [1,4,2,5,3] 输出:11 解释:有 11 个有效子数组,分别是:[1],[4],[2],[5],[3],[1,4],[2,5],[1,4,2],[2,5,3],[1,4,2,5],[1,4,2,5,3] 。
示例 2:
输入:nums = [3,2,1] 输出:3 解释:有 3 个有效子数组,分别是:[3],[2],[1] 。
示例 3:
输入:nums = [2,2,2] 输出:6 解释:有 6 个有效子数组,分别为是:[2],[2],[2],[2,2],[2,2],[2,2,2] 。
提示:
1 <= nums.length <= 5 * 104
0 <= nums[i] <= 105
题目实际上是求解每个位置
我们可以使用单调栈来求解右边第一个小于
接下来,我们将
时间复杂度
class Solution:
def validSubarrays(self, nums: List[int]) -> int:
n = len(nums)
right = [n] * n
stk = []
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] >= nums[i]:
stk.pop()
if stk:
right[i] = stk[-1]
stk.append(i)
return sum(j - i for i, j in enumerate(right))
class Solution {
public int validSubarrays(int[] nums) {
int n = nums.length;
int[] right = new int[n];
Arrays.fill(right, n);
Deque<Integer> stk = new ArrayDeque<>();
for (int i = n - 1; i >= 0; --i) {
while (!stk.isEmpty() && nums[stk.peek()] >= nums[i]) {
stk.pop();
}
if (!stk.isEmpty()) {
right[i] = stk.peek();
}
stk.push(i);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += right[i] - i;
}
return ans;
}
}
class Solution {
public:
int validSubarrays(vector<int>& nums) {
int n = nums.size();
vector<int> right(n, n);
stack<int> stk;
for (int i = n - 1; ~i; --i) {
while (stk.size() && nums[stk.top()] >= nums[i]) {
stk.pop();
}
if (stk.size()) {
right[i] = stk.top();
}
stk.push(i);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += right[i] - i;
}
return ans;
}
};
func validSubarrays(nums []int) (ans int) {
n := len(nums)
right := make([]int, n)
for i := range right {
right[i] = n
}
stk := []int{}
for i := n - 1; i >= 0; i-- {
for len(stk) > 0 && nums[stk[len(stk)-1]] >= nums[i] {
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
right[i] = stk[len(stk)-1]
}
stk = append(stk, i)
}
for i, j := range right {
ans += j - i
}
return
}
function validSubarrays(nums: number[]): number {
const n = nums.length;
const right: number[] = Array(n).fill(n);
const stk: number[] = [];
for (let i = n - 1; ~i; --i) {
while (stk.length && nums[stk.at(-1)] >= nums[i]) {
stk.pop();
}
if (stk.length) {
right[i] = stk.at(-1)!;
}
stk.push(i);
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += right[i] - i;
}
return ans;
}
class Solution:
def validSubarrays(self, nums: List[int]) -> int:
n = len(nums)
stk = []
ans = 0
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] >= nums[i]:
stk.pop()
ans += (stk[-1] if stk else n) - i
stk.append(i)
return ans
class Solution {
public int validSubarrays(int[] nums) {
int n = nums.length;
Deque<Integer> stk = new ArrayDeque<>();
int ans = 0;
for (int i = n - 1; i >= 0; --i) {
while (!stk.isEmpty() && nums[stk.peek()] >= nums[i]) {
stk.pop();
}
ans += (stk.isEmpty() ? n : stk.peek()) - i;
stk.push(i);
}
return ans;
}
}
class Solution {
public:
int validSubarrays(vector<int>& nums) {
int n = nums.size();
stack<int> stk;
int ans = 0;
for (int i = n - 1; ~i; --i) {
while (stk.size() && nums[stk.top()] >= nums[i]) {
stk.pop();
}
ans += (stk.size() ? stk.top() : n) - i;
stk.push(i);
}
return ans;
}
};
func validSubarrays(nums []int) (ans int) {
n := len(nums)
stk := []int{}
for i := n - 1; i >= 0; i-- {
for len(stk) > 0 && nums[stk[len(stk)-1]] >= nums[i] {
stk = stk[:len(stk)-1]
}
ans -= i
if len(stk) > 0 {
ans += stk[len(stk)-1]
} else {
ans += n
}
stk = append(stk, i)
}
return
}
function validSubarrays(nums: number[]): number {
const n = nums.length;
const stk: number[] = [];
let ans = 0;
for (let i = n - 1; ~i; --i) {
while (stk.length && nums[stk.at(-1)!] >= nums[i]) {
stk.pop();
}
ans += (stk.at(-1) ?? n) - i;
stk.push(i);
}
return ans;
}