[LeetCode] Remove One Element to Make the Array Strictly Increasing

1909. Remove One Element to Make the Array Strictly Increasing

nums, return true if it can be made strictly increasing after removing exactly one element, or false otherwise. If the array is already strictly increasing, return true.

The array nums``nums[i - 1] < nums[i] for each index (1 <= i < nums.length).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
bool canBeIncreasing(vector<int>& nums) {
vector<int> ok(nums.size());
ok[0] = true;
for(int i = 1; i < nums.size(); i++) {
if(nums[i-1] >= nums[i]) break;
ok[i] = true;
}
if(ok.back()) return true;
if(ok[nums.size()-2]) return true;
for(int i = nums.size() - 1; i >= 1; i--) {
if(nums[i-1] >= nums[i]) {
if(i == 1) return true;
if(!ok[i-2]) return false;
return nums[i-2] < nums[i];
}
if(i >= 2 and nums[i-2] < nums[i] and ok[i-2]) return true;
}
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/09/PS/LeetCode/remove-one-element-to-make-the-array-strictly-increasing/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.