Shortest Block to Cut for a Non-Decreasing Remainder
Arrays & HashingAuto-graded · 25 test casesLast updated
Find the shortest contiguous block whose removal leaves a non-decreasing array, using sorted prefix/suffix boundaries and two pointers, with a smallest-start tie-break.
You are given an integer list nums. You may delete one contiguous block of elements nums[L..R] (both ends inclusive). The elements left over keep their original relative order and are joined together.
Find the shortest block whose deletion leaves a non-decreasing sequence (each element is greater than or equal to the one before it). Return it as the list [L, R].
Rules:
- If
numsis already non-decreasing (this includes an empty list and a single element), nothing needs to be deleted. Return[-1, -1]. - Otherwise the block must be non-empty, with
0 <= L <= R < len(nums). - Deleting the block may leave an empty remainder, or only a prefix, or only a suffix. All of these count as non-decreasing.
- If several blocks share the minimum length, return the one with the smallest
L. This is the same as choosing the lexicographically smallest pair[L, R], because equal lengths mean a smallerLgives a smallerR. - Equal neighbours are allowed after the join, so a kept prefix value may equal the first kept suffix value.
Your solution should run in O(n) time with O(1) extra space.
Constraints
0 <= len(nums) <= 2 * 10^5- Each element is an integer, and may be negative or large in magnitude.
- The return value is a list of two integers
[L, R], or[-1, -1]if no deletion is needed.
Examples
Example 1
Input: find_shortest_cut([1, 2, 3, 10, 4, 2, 3, 5])
Output: [2, 4]
Example 2
Input: find_shortest_cut([5, 4, 3, 2, 1])
Output: [0, 3]
Example 3
Input: find_shortest_cut([1, 2, 3])
Output: [-1, -1]
Hints
Hint 1
After deleting a block, what remains is a prefix of the array plus a suffix of the array. Both must already be non-decreasing on their own, so find the longest sorted prefix and the longest sorted suffix first.
Hint 2
If you keep the prefix up to index i, the kept suffix must start at some index j inside the sorted suffix with nums[j] >= nums[i]. For a fixed i you want the smallest such j.
Hint 3
As i grows, nums[i] never decreases within the sorted prefix, so the best j never moves left. Use a single pointer for j, and only replace the best answer on a strictly shorter block so the smallest L wins ties. Do not forget i = -1, which means keeping no prefix.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Deleting nums[L..R] leaves a prefix nums[0..L-1] and a suffix nums[R+1..n-1]. The remainder is non-decreasing exactly when:
- the kept prefix is non-decreasing,
- the kept suffix is non-decreasing, and
- the last kept prefix value is
<=the first kept suffix value (skipped if either side is empty).
Boundaries. Scan from the left to find prefix_end, the last index of the longest non-decreasing prefix. If it is n - 1, the array is already sorted, so return [-1, -1]. Scan from the right to find suffix_start, the first index of the longest non-decreasing suffix.
Search. Let i be the last kept prefix index (-1 for none) and j the first kept suffix index (n for none). Conditions 1 and 2 force i <= prefix_end and j >= suffix_start. The deleted block is [i+1, j-1] with length j - i - 1.
- For
i = -1, we keep only a suffix, so the best choice isj = suffix_start. This gives the block[0, suffix_start - 1]. - For each
iin0..prefix_end, the bestjis the smallest index>= suffix_startwithnums[j] >= nums[i], ornif there is none.
Why it is correct
- Every valid deletion has
iin[-1, prefix_end]andjin[suffix_start, n]. Otherwise the kept part would contain the descent atprefix_endor the one just beforesuffix_start. - For a fixed
i, any validjmust be at least the smallest one satisfyingnums[j] >= nums[i]. The suffix is sorted, so every laterjalso works, and the smallest gives the shortest block for thati. - Within the sorted prefix,
nums[i]never decreases asigrows, so the threshold only rises and the bestjonly moves right. One pointer that never moves backward therefore finds all of them. - The block is never empty, because
nums[prefix_end] > nums[prefix_end + 1]makesj = prefix_end + 1invalid fori = prefix_end. - Tie-breaking: for a fixed length, a smaller
Lmeans a smaller pair. The candidates are visited in increasingL(starting fromi = -1, soL = 0), and the best is replaced only on a strictly shorter block. The first optimum found therefore has the smallestL.
Complexity
- Time: O(n), since the two boundary scans and the pointer
jeach move at mostnsteps in total. - Space: O(1) extra.
Reference solution
from typing import List
def find_shortest_cut(nums: List[int]) -> List[int]:
n = len(nums)
if n == 0:
return [-1, -1]
# Last index of the longest non-decreasing prefix.
prefix_end = 0
while prefix_end + 1 < n and nums[prefix_end] <= nums[prefix_end + 1]:
prefix_end += 1
if prefix_end == n - 1:
return [-1, -1]
# First index of the longest non-decreasing suffix.
suffix_start = n - 1
while suffix_start > 0 and nums[suffix_start - 1] <= nums[suffix_start]:
suffix_start -= 1
# i = last kept prefix index (-1 means keep no prefix).
# j = first kept suffix index (n means keep no suffix).
best_l, best_r = 0, suffix_start - 1
best_len = best_r - best_l + 1
j = suffix_start
for i in range(0, prefix_end + 1):
while j < n and nums[j] < nums[i]:
j += 1
length = j - i - 1
if length < best_len:
best_len = length
best_l, best_r = i + 1, j - 1
return [best_l, best_r]