Skip to content

Add Solution to LC Problem 1909 #3826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
id: remove-one-element-to-make-array-strictly-increasing
title: Remove One Element to Make the Array Strictly Increasing
sidebar_label: Remove One Element to Make Array Strictly Increasing
tags: [Array, Greedy, C++, Python, Java]
description: Solve the problem of determining whether an array can be made strictly increasing by removing exactly one element.
---

## Problem Statement

### Problem Description

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`.

The array `nums` is strictly increasing if `nums[i - 1] < nums[i]` for each index `1 <= i < nums.length`.

### Example

**Example 1:**

```
Input: nums = [1, 2, 10, 5, 7]
Output: true
```
**Explanation:** By removing 10 at index 2 from nums, it becomes [1, 2, 5, 7].
[1, 2, 5, 7] is strictly increasing, so return true.


**Example 2:**
```
Input: nums = [2, 3, 1, 2]
Output: false
```
**Explanation:**
[3, 1, 2] is the result of removing the element at index 0.
[2, 1, 2] is the result of removing the element at index 1.
[2, 3, 2] is the result of removing the element at index 2.
[2, 3, 1] is the result of removing the element at index 3.
No resulting array is strictly increasing, so return false.


### Constraints

- 2 <= `nums.length` <= 1000
- 1 <= `nums[i]` <= 1000

## Solution

### Intuition

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`.

### Time Complexity and Space Complexity Analysis

- **Time Complexity**:
- 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.

- **Space Complexity**:
- The space complexity is $O(1)$ as we are only using a fixed amount of extra space for variables.

### Code

#### C++

```cpp
class Solution {
public:
bool canBeIncreasing(vector<int>& nums) {
int count = 0;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] <= nums[i - 1]) {
count++;
if (count > 1) return false;
if (i > 1 && nums[i] <= nums[i - 2] && i < nums.size() - 1 && nums[i + 1] <= nums[i - 1])
return false;
}
}
return true;
}
};
```

#### Python
```python
class Solution:
def canBeIncreasing(self, nums: List[int]) -> bool:
count = 0
for i in range(1, len(nums)):
if nums[i] <= nums[i - 1]:
count += 1
if count > 1:
return False
if i > 1 and nums[i] <= nums[i - 2] and i < len(nums) - 1 and nums[i + 1] <= nums[i - 1]:
return False
return True
```

#### Java
```java
class Solution {
public boolean canBeIncreasing(int[] nums) {
int count = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] <= nums[i - 1]) {
count++;
if (count > 1) {
return false;
}
if (i > 1 && nums[i] <= nums[i - 2] && i < nums.length - 1 && nums[i + 1] <= nums[i - 1]) {
return false;
}
}
}
return true;
}
}
```
Loading