2970. 统计移除递增子数组的数目 I
题目链接:2970. 统计移除递增子数组的数目 I
代码如下:
//参考链接:https://leetcode.cn/problems/count-the-number-of-incremovable-subarrays-i/solutions/2577665/shuang-zhi-zhen-on-shi-jian-o1-kong-jian-3fl2
class Solution
{
public:
int incremovableSubarrayCount(vector<int>& nums)
{
int i=0;
for(i;i<nums.size()-1&&nums[i]<nums[i+1];i++);
if(i==nums.size()-1) {return nums.size()*(nums.size()+1)/2;}
int res=i+2;
for(int j=nums.size()-1;j==nums.size()-1||nums[j]<nums[j+1];j--)
{
while(i>=0&&nums[i]>=nums[j])
{
i--;
}
res+=i+2;
}
return res;
}
};