1909. Remove One Element to Make the Array Strictly Increasing

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
class Solution {
public boolean canBeIncreasing(int[] nums) {
int max = nums[0];

int replaced = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] <= max) {
// 出现了更小的数,删除当前数字还是前一个数字?目标:删除数字后 max 尽可能小
if (++replaced > 1) {
return false;
}

if (nums[i] < max) {
if (i - 2 < 0 || nums[i] > nums[i - 2]) {
// 删除之前的元素,重新订正 max
max = nums[i];
}
}
} else {
max = nums[i];
}
}

return true;
}
}

References

1909. Remove One Element to Make the Array Strictly Increasing