forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-the-number-of-incremovable-subarrays-i.cpp
67 lines (65 loc) · 1.83 KB
/
count-the-number-of-incremovable-subarrays-i.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Time: O(n)
// Space: O(1)
// two pointers
class Solution {
public:
long long incremovableSubarrayCount(vector<int>& nums) {
int j = size(nums) - 1;
for (; j >= 1; --j) {
if (!(nums[j - 1] < nums[j])) {
break;
}
}
if (j == 0) {
return (size(nums) + 1) * size(nums) / 2;
}
int64_t result = size(nums) - j + 1;
for (int i = 0; i + 1 < size(nums); ++i) {
while (j < size(nums) && !(nums[i] < nums[j])) {
++j;
}
result += size(nums) - j + 1;
if (!(nums[i] < nums[i + 1])) {
break;
}
}
return result;
}
};
// Time: O(n^3)
// Space: O(1)
// brute force
class Solution2 {
public:
long long incremovableSubarrayCount(vector<int>& nums) {
int64_t result = 0;
for (int left = 0; left < size(nums); ++left) {
for (int right = left; right < size(nums); ++right) {
bool check = left == 0 || right == size(nums) - 1 || nums[left - 1] < nums[right + 1];
if (!check) {
continue;
}
for (int i = 0; i + 1 < left; ++i) {
if (!(nums[i] < nums[i + 1])) {
check = false;
break;
}
}
if (!check) {
continue;
}
for (int i = right + 1; i + 1 < size(nums); ++i) {
if (!(nums[i] < nums[i + 1])) {
check = false;
break;
}
}
if (!check) {
continue;
}
++result;
}
}
return result;
}
};