-
Notifications
You must be signed in to change notification settings - Fork 0
/
incremovableSubarrays.txt
35 lines (35 loc) · 1.27 KB
/
incremovableSubarrays.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2970. Count the Number of Incremovable Subarrays I
/*
You are given a 0-indexed array of positive integers nums.
A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7]
because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
Return the total number of incremovable subarrays of nums.
Note that an empty array is considered strictly increasing.
A subarray is a contiguous non-empty sequence of elements within an array.
*\
class Solution {
public:
int incremovableSubarrayCount(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < nums.size(); i++)
{
for (int j = 0; j < nums.size(); j++)
{
int last = 0, flag = 1;
for (int k = 0; k < nums.size(); k++)
{
if(k >= i && k <= j){
continue;
}
if(nums[k] >= last){
flag = 0;
return;
}
last = nums[k];
ans++;
}
}
}
return ans;
}
};