Skip to content

Commit 4a283e9

Browse files
committed
feat: add solutions to lc problem: No.1909. Remove One Element to Make
the Array Strictly Increasing
1 parent 0f671a5 commit 4a283e9

File tree

6 files changed

+246
-4
lines changed

6 files changed

+246
-4
lines changed

solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md

+85-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
5656
</ul>
5757

58-
5958
## 解法
6059

6160
<!-- 这里可写通用的实现逻辑 -->
@@ -67,15 +66,99 @@
6766
<!-- 这里可写当前语言的特殊实现逻辑 -->
6867

6968
```python
70-
69+
class Solution:
70+
def canBeIncreasing(self, nums: List[int]) -> bool:
71+
def check(nums, i):
72+
prev = float('-inf')
73+
for j, num in enumerate(nums):
74+
if i == j:
75+
continue
76+
if prev >= nums[j]:
77+
return False
78+
prev = nums[j]
79+
return True
80+
81+
i, n = 1, len(nums)
82+
while i < n and nums[i - 1] < nums[i]:
83+
i += 1
84+
return check(nums, i - 1) or check(nums, i)
7185
```
7286

7387
### **Java**
7488

7589
<!-- 这里可写当前语言的特殊实现逻辑 -->
7690

7791
```java
92+
class Solution {
93+
public boolean canBeIncreasing(int[] nums) {
94+
int i = 1, n = nums.length;
95+
for (; i < n && nums[i - 1] < nums[i]; ++i);
96+
return check(nums, i - 1) || check(nums, i);
97+
}
98+
99+
private boolean check(int[] nums, int i) {
100+
int prev = Integer.MIN_VALUE;
101+
for (int j = 0; j < nums.length; ++j) {
102+
if (i == j) {
103+
continue;
104+
}
105+
if (prev >= nums[j]) {
106+
return false;
107+
}
108+
prev = nums[j];
109+
}
110+
return true;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
bool canBeIncreasing(vector<int>& nums) {
121+
int i = 1, n = nums.size();
122+
for (; i < n && nums[i - 1] < nums[i]; ++i);
123+
return check(nums, i - 1) || check(nums, i);
124+
}
125+
126+
bool check(vector<int>& nums, int i) {
127+
int prev = 0;
128+
for (int j = 0; j < nums.size(); ++j) {
129+
if (i == j) continue;
130+
if (prev >= nums[j]) return false;
131+
prev = nums[j];
132+
}
133+
return true;
134+
}
135+
};
136+
```
78137

138+
### **Go**
139+
140+
```go
141+
func canBeIncreasing(nums []int) bool {
142+
i, n := 1, len(nums)
143+
for ; i < n && nums[i-1] < nums[i]; i++ {
144+
145+
}
146+
return check(nums, i-1) || check(nums, i)
147+
}
148+
149+
func check(nums []int, i int) bool {
150+
prev := 0
151+
for j := 0; j < len(nums); j++ {
152+
if i == j {
153+
continue
154+
}
155+
if prev >= nums[j] {
156+
return false
157+
}
158+
prev = nums[j]
159+
}
160+
return true
161+
}
79162
```
80163

81164
### **...**

solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md

+85-2
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,104 @@ No resulting array is strictly increasing, so return false.</pre>
5555
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
5656
</ul>
5757

58-
5958
## Solutions
6059

6160
<!-- tabs:start -->
6261

6362
### **Python3**
6463

6564
```python
66-
65+
class Solution:
66+
def canBeIncreasing(self, nums: List[int]) -> bool:
67+
def check(nums, i):
68+
prev = float('-inf')
69+
for j, num in enumerate(nums):
70+
if i == j:
71+
continue
72+
if prev >= nums[j]:
73+
return False
74+
prev = nums[j]
75+
return True
76+
77+
i, n = 1, len(nums)
78+
while i < n and nums[i - 1] < nums[i]:
79+
i += 1
80+
return check(nums, i - 1) or check(nums, i)
6781
```
6882

6983
### **Java**
7084

7185
```java
86+
class Solution {
87+
public boolean canBeIncreasing(int[] nums) {
88+
int i = 1, n = nums.length;
89+
for (; i < n && nums[i - 1] < nums[i]; ++i);
90+
return check(nums, i - 1) || check(nums, i);
91+
}
92+
93+
private boolean check(int[] nums, int i) {
94+
int prev = Integer.MIN_VALUE;
95+
for (int j = 0; j < nums.length; ++j) {
96+
if (i == j) {
97+
continue;
98+
}
99+
if (prev >= nums[j]) {
100+
return false;
101+
}
102+
prev = nums[j];
103+
}
104+
return true;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
bool canBeIncreasing(vector<int>& nums) {
115+
int i = 1, n = nums.size();
116+
for (; i < n && nums[i - 1] < nums[i]; ++i);
117+
return check(nums, i - 1) || check(nums, i);
118+
}
119+
120+
bool check(vector<int>& nums, int i) {
121+
int prev = 0;
122+
for (int j = 0; j < nums.size(); ++j) {
123+
if (i == j) continue;
124+
if (prev >= nums[j]) return false;
125+
prev = nums[j];
126+
}
127+
return true;
128+
}
129+
};
130+
```
72131

132+
### **Go**
133+
134+
```go
135+
func canBeIncreasing(nums []int) bool {
136+
i, n := 1, len(nums)
137+
for ; i < n && nums[i-1] < nums[i]; i++ {
138+
139+
}
140+
return check(nums, i-1) || check(nums, i)
141+
}
142+
143+
func check(nums []int, i int) bool {
144+
prev := 0
145+
for j := 0; j < len(nums); j++ {
146+
if i == j {
147+
continue
148+
}
149+
if prev >= nums[j] {
150+
return false
151+
}
152+
prev = nums[j]
153+
}
154+
return true
155+
}
73156
```
74157

75158
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool canBeIncreasing(vector<int>& nums) {
4+
int i = 1, n = nums.size();
5+
for (; i < n && nums[i - 1] < nums[i]; ++i);
6+
return check(nums, i - 1) || check(nums, i);
7+
}
8+
9+
bool check(vector<int>& nums, int i) {
10+
int prev = 0;
11+
for (int j = 0; j < nums.size(); ++j) {
12+
if (i == j) continue;
13+
if (prev >= nums[j]) return false;
14+
prev = nums[j];
15+
}
16+
return true;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func canBeIncreasing(nums []int) bool {
2+
i, n := 1, len(nums)
3+
for ; i < n && nums[i-1] < nums[i]; i++ {
4+
5+
}
6+
return check(nums, i-1) || check(nums, i)
7+
}
8+
9+
func check(nums []int, i int) bool {
10+
prev := 0
11+
for j := 0; j < len(nums); j++ {
12+
if i == j {
13+
continue
14+
}
15+
if prev >= nums[j] {
16+
return false
17+
}
18+
prev = nums[j]
19+
}
20+
return true
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean canBeIncreasing(int[] nums) {
3+
int i = 1, n = nums.length;
4+
for (; i < n && nums[i - 1] < nums[i]; ++i);
5+
return check(nums, i - 1) || check(nums, i);
6+
}
7+
8+
private boolean check(int[] nums, int i) {
9+
int prev = Integer.MIN_VALUE;
10+
for (int j = 0; j < nums.length; ++j) {
11+
if (i == j) {
12+
continue;
13+
}
14+
if (prev >= nums[j]) {
15+
return false;
16+
}
17+
prev = nums[j];
18+
}
19+
return true;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def canBeIncreasing(self, nums: List[int]) -> bool:
3+
def check(nums, i):
4+
prev = float('-inf')
5+
for j, num in enumerate(nums):
6+
if i == j:
7+
continue
8+
if prev >= nums[j]:
9+
return False
10+
prev = nums[j]
11+
return True
12+
13+
i, n = 1, len(nums)
14+
while i < n and nums[i - 1] < nums[i]:
15+
i += 1
16+
return check(nums, i - 1) or check(nums, i)

0 commit comments

Comments
 (0)