↓Skip to main content
  1. LeetCode/

LeetCode 3818: Minimum Prefix Removal to Make Array Strictly Increasing

·1 min· ·
LeetCode Medium Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-25

  • Submissions: Accepted (first try)

Approach
#

What remains is a suffix that must be strictly increasing. A longer suffix means a shorter removed prefix, so walk left from the end while nums[i - 1] < nums[i] (Array Template 1). Where it stops, i, is the length to remove.

My Solution
#

class Solution:
    def minimumPrefixLength(self, nums: List[int]) -> int:
        i = len(nums) - 1
        while i > 0 and nums[i - 1] < nums[i]:
            i -= 1
        return i

I originally wrote while ... else: return right; with no break the else always runs, so it is equivalent, but returning directly is clearer.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Strictly increasing: stop at equal values too.

Interview Explanation
#

The remainder must be a strictly increasing suffix, so I walk left until the first non-increasing step; that index is the prefix length to remove. Time O(n).

Related

LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted
·2 mins
LeetCode Medium Two-Pointers Binary-Search Array Monotonic-Stack
LeetCode 1574 note: what remains is a non-decreasing prefix plus a non-decreasing suffix; find the longest of each, then join them with two pointers
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 2105: Watering Plants II
·2 mins
LeetCode Medium Two-Pointers Simulation Array
LeetCode 2105 note: Alice from the left and Bob from the right, simulated with converging pointers; when they meet at the same plant, whoever has more water waters it
LeetCode 2401: Longest Nice Subarray
·2 mins
LeetCode Medium Sliding-Window Bit-Manipulation Array
LeetCode 2401 note: pairwise AND 0 ⇔ each bit is used by at most one number; a mask holds the window’s bits, and on a conflict the left shrinks and its bits are removed
LeetCode 930: Binary Subarrays With Sum
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 930 note: “sum exactly goal” = “sum at most goal” − “sum at most goal - 1”; or prefix sums with a hash map
LeetCode 2962: Count Subarrays Where Max Element Appears at Least K Times
·2 mins
LeetCode Medium Sliding-Window Array
LeetCode 2962 note: an “at least k” counting window: keep shrinking while the max appears k times, after which every left end in [0, left) is valid, so count += left