Skip to content

Commit c83f6df

Browse files
authored
Merge pull request #3826 from revanth1718/main
Add Solution to LC Problem 1909
2 parents 805b824 + 38d0b6e commit c83f6df

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
id: remove-one-element-to-make-array-strictly-increasing
3+
title: Remove One Element to Make the Array Strictly Increasing
4+
sidebar_label: Remove One Element to Make Array Strictly Increasing
5+
tags: [Array, Greedy, C++, Python, Java]
6+
description: Solve the problem of determining whether an array can be made strictly increasing by removing exactly one element.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
Given a 0-indexed integer array `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`.
14+
15+
The array `nums` is strictly increasing if `nums[i - 1] < nums[i]` for each index `1 <= i < nums.length`.
16+
17+
### Example
18+
19+
**Example 1:**
20+
21+
```
22+
Input: nums = [1, 2, 10, 5, 7]
23+
Output: true
24+
```
25+
**Explanation:** By removing 10 at index 2 from nums, it becomes [1, 2, 5, 7].
26+
[1, 2, 5, 7] is strictly increasing, so return true.
27+
28+
29+
**Example 2:**
30+
```
31+
Input: nums = [2, 3, 1, 2]
32+
Output: false
33+
```
34+
**Explanation:**
35+
[3, 1, 2] is the result of removing the element at index 0.
36+
[2, 1, 2] is the result of removing the element at index 1.
37+
[2, 3, 2] is the result of removing the element at index 2.
38+
[2, 3, 1] is the result of removing the element at index 3.
39+
No resulting array is strictly increasing, so return false.
40+
41+
42+
### Constraints
43+
44+
- 2 <= `nums.length` <= 1000
45+
- 1 <= `nums[i]` <= 1000
46+
47+
## Solution
48+
49+
### Intuition
50+
51+
To solve this problem, we can use a greedy approach by iterating through the array and checking if there are any elements that violate the strictly increasing condition. If we find such an element, we can attempt to remove either the current element or the previous element and check if the resulting array (excluding that element) is strictly increasing. If any such removal results in a strictly increasing array, we return `true`. Otherwise, we return `false`.
52+
53+
### Time Complexity and Space Complexity Analysis
54+
55+
- **Time Complexity**:
56+
- The solution involves a single pass through the array and a few constant-time checks, resulting in an overall time complexity of $O(n)$, where `n` is the length of the array.
57+
58+
- **Space Complexity**:
59+
- The space complexity is $O(1)$ as we are only using a fixed amount of extra space for variables.
60+
61+
### Code
62+
63+
#### C++
64+
65+
```cpp
66+
class Solution {
67+
public:
68+
bool canBeIncreasing(vector<int>& nums) {
69+
int count = 0;
70+
for (int i = 1; i < nums.size(); ++i) {
71+
if (nums[i] <= nums[i - 1]) {
72+
count++;
73+
if (count > 1) return false;
74+
if (i > 1 && nums[i] <= nums[i - 2] && i < nums.size() - 1 && nums[i + 1] <= nums[i - 1])
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
80+
};
81+
```
82+
83+
#### Python
84+
```python
85+
class Solution:
86+
def canBeIncreasing(self, nums: List[int]) -> bool:
87+
count = 0
88+
for i in range(1, len(nums)):
89+
if nums[i] <= nums[i - 1]:
90+
count += 1
91+
if count > 1:
92+
return False
93+
if i > 1 and nums[i] <= nums[i - 2] and i < len(nums) - 1 and nums[i + 1] <= nums[i - 1]:
94+
return False
95+
return True
96+
```
97+
98+
#### Java
99+
```java
100+
class Solution {
101+
public boolean canBeIncreasing(int[] nums) {
102+
int count = 0;
103+
for (int i = 1; i < nums.length; i++) {
104+
if (nums[i] <= nums[i - 1]) {
105+
count++;
106+
if (count > 1) {
107+
return false;
108+
}
109+
if (i > 1 && nums[i] <= nums[i - 2] && i < nums.length - 1 && nums[i + 1] <= nums[i - 1]) {
110+
return false;
111+
}
112+
}
113+
}
114+
return true;
115+
}
116+
}
117+
```

0 commit comments

Comments
 (0)