Skip to content

Commit a737eba

Browse files
authored
Create count-the-number-of-incremovable-subarrays-ii.cpp
1 parent 64f03e5 commit a737eba

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// two pointers
5+
class Solution {
6+
public:
7+
long long incremovableSubarrayCount(vector<int>& nums) {
8+
int j = size(nums) - 1;
9+
for (; j >= 1; --j) {
10+
if (!(nums[j - 1] < nums[j])) {
11+
break;
12+
}
13+
}
14+
if (j == 0) {
15+
return (size(nums) + 1) * size(nums) / 2;
16+
}
17+
int64_t result = size(nums) - j + 1;
18+
for (int i = 0; i + 1 < size(nums); ++i) {
19+
while (j < size(nums) && !(nums[i] < nums[j])) {
20+
++j;
21+
}
22+
result += size(nums) - j + 1;
23+
if (!(nums[i] < nums[i + 1])) {
24+
break;
25+
}
26+
}
27+
return result;
28+
}
29+
};

0 commit comments

Comments
 (0)