EasyNo. 1909

Remove One Element to Make the Array Strictly Increasing — LeetCode 1909 Python Solution

Topics
Array
Reading time
3 min

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

PythonSolution
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

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.