File: remove-one-element-to-make-the-array-strictly-increasing/solution.py

Date: 2026-06-06

Time: 18:48

Purpose

This file solves LeetCode 1909: Remove One Element to Make the Array Strictly Increasing. It determines whether you can remove exactly one element from an integer array to make the remaining elements strictly increasing. It's a standalone solution module in a repo of ~500+ LeetCode problems, each in its own directory.

Key Components

canBeIncreasing(nums: list[int]) -> bool

The public entry point. Scans for the first violation of strict monotonicity, then checks whether removing either of the two elements involved in the violation fixes the array.

isincreasingskip(skip: int) -> bool

A nested helper that checks whether nums is strictly increasing when the element at index skip is logically removed. Instead of building a new list, it iterates through all indices and skips the one at skip, comparing each non-skipped value against the previous.

Patterns

Early violation detection with targeted repair. Rather than brute-forcing all n possible removals (O(n^2)), the algorithm finds the *first* pair (i-1, i) where nums[i] <= nums[i-1] and only tests removing one of those two. This works because any fix *must* address the first violation — if neither removal fixes it, no single removal can fix the array.

Logical deletion over physical deletion. isincreasingskip doesn't slice or copy the array. It skips the target index during iteration, keeping the check O(n) with O(1) extra space.

Sentinel initialization. prev starts at -1, which works because LeetCode's constraints guarantee nums[i] >= 1. This avoids special-casing the first element comparison.

Dependencies

Imports: None — pure Python, no stdlib or third-party dependencies.

Imported by: The "Imported By" list in the prompt is misleading — those are unrelated test files across the repo, likely an artifact of the analysis tool. The actual consumer is remove-one-element-to-make-the-array-strictly-increasing/test_solution.py.

Flow

1. Iterate i from 1 to len(nums) - 1.

2. At each step, check if nums[i] <= nums[i-1] (violation of strict increase).

3. On the first violation found, return isincreasingskip(i) or isincreasingskip(i-1) — try removing the current element or the previous one.

4. If the loop completes with no violation, the array is already strictly increasing. Removing any single element preserves this, so return True.

The two isincreasingskip calls each do a full O(n) pass, but they run at most once total, so overall complexity is O(n).

Invariants

Error Handling

None. The function trusts that it receives a valid list[int] matching the problem constraints. No exceptions are raised or caught.

Topics to Explore

Beliefs