Skip to content

Commit 32195d9

Browse files
solves remove one element to make the array strictly increasing
1 parent 77f055a commit 32195d9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@
459459
| 1893 | [Check if All the Integers in a Range Are Covered](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered) | [![Java](assets/java.png)](src/CheckIfAllTheIntegersInARangeAreCovered.java) | |
460460
| 1897 | [Redistribute Characters to Make All Strings Equal](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal) | [![Java](assets/java.png)](src/RedistributeCharactersToMakeAllStringsEqual.java) | |
461461
| 1903 | [Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string) | [![Java](assets/java.png)](src/LargestOddNumberInString.java) | |
462-
| 1909 | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing) | | |
462+
| 1909 | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing) | [![Java](assets/java.png)](src/RemoveOneElementToMakeTheArrayStrictlyIncreasing.java) | |
463463
| 1913 | [Maximum Product Difference Between Two Pairs](https://leetcode.com/problems/maximum-product-difference-between-two-pairs) | | |
464464
| 1920 | [Build Array from Permutation](https://leetcode.com/problems/build-array-from-permutation) | | |
465465
| 1925 | [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples) | | |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class RemoveOneElementToMakeTheArrayStrictlyIncreasing {
2+
public boolean canBeIncreasing(int[] array) {
3+
if (array[0] >= array[1]) {
4+
if (array.length == 2) return true;
5+
return array[2] > array[1] && isStrictlyIncreasing(array, 3);
6+
}
7+
boolean firstAnomaly = true;
8+
for (int index = 2 ; index < array.length ; index++) {
9+
if (array[index] <= array[index - 1]) {
10+
if (!firstAnomaly) return false;
11+
firstAnomaly = false;
12+
if (index == array.length - 1) return true;
13+
if (array[index + 1] > array[index - 1]) continue;
14+
if (array[index + 1] > array[index] && array[index] > array[index - 2]) continue;
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
21+
private boolean isStrictlyIncreasing(int[] array, int start) {
22+
for (int i = start ; i < array.length ; i++) {
23+
if (array[i] <= array[i - 1]) return false;
24+
}
25+
return true;
26+
}
27+
}

0 commit comments

Comments
 (0)