EasyNo. 1909
Remove One Element to Make the Array Strictly Increasing — LeetCode 1909 Python Solution
- Topics
- Array
- Reading time
- 3 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- nums = [1,2,10,5,7]
- Output
- true
- Explanation
- By removing 10 at index 2 from nums, it becomes [1,2,5,7].
Python solution
class Solution:
def canBeIncreasing(self, nums: List[int]) -> bool:
def check(k: int) -> bool:
pre = -inf
for i, x in enumerate(nums):
if i == k:
continue
if pre >= x:
return False
pre = x
return True
i = 0
while i + 1 < len(nums) and nums[i] < nums[i + 1]:
i += 1
return check(i) or check(i + 1)Complexity
- Time
- O(n)where n is the length of the array nums
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 1909. Remove One Element to Make the Array Strictly Increasing?
LeetCode 1909. Remove One Element to Make the Array Strictly Increasing is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1909. Remove One Element to Make the Array Strictly Increasing?
The Python solution on this page runs in O(n), where n is the length of the array \textit{nums}.
- What is the space complexity of LeetCode 1909. Remove One Element to Make the Array Strictly Increasing?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1909. Remove One Element to Make the Array Strictly Increasing cover?
LeetCode 1909. Remove One Element to Make the Array Strictly Increasing is tagged Array on LeetCode.