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