[LeetCode] Number of Valid Subarrays

1063. Number of Valid Subarrays

Given an array nums of integers, return the number of non-empty continuous subarrays that satisfy the following condition:

The leftmost element of the subarray is not larger than other elements in the subarray.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int validSubarrays(vector<int>& nums) {
multiset<int> s;
int res = 0;
for(auto &n : nums) {
s.erase(next(s.insert(n)), s.end());
res += s.size();
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/07/10/PS/LeetCode/number-of-valid-subarrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.