-
Notifications
You must be signed in to change notification settings - Fork 0
/
day 1 prob 1
68 lines (51 loc) · 2.17 KB
/
day 1 prob 1
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
68
https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/
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.
Example 1:
Input: nums = [1,2,3,4]
Output: 10
Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
Example 2:
Input: nums = [6,5,7,8]
Output: 7
Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
Example 3:
Input: nums = [8,7,6,6]
Output: 3
Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 50
answer code
class Solution {
public:
int cheakinc(vector<int>& nums, int i, int j){
int t=0;
int prv = 0;
while(t < nums.size()){
if(t >= i && t <= j) t++;
else{
if(nums[t] <= prv) return 0;
else{
prv = nums[t];
t++;
}
}
}
return 1;
}
int incremovableSubarrayCount(vector<int>& nums) {
int ans = 0;
for(int i = 0; i < nums.size(); i++){
for(int j = i; j < nums.size(); j++){
if(cheakinc(nums, i, j)){ ans++; cout<<i<<" "<<j<<endl;}
}
}
return ans;
}
};