↓Skip to main content
  1. LeetCode/

LeetCode 2009: Minimum Number of Operations to Make Array Continuous

·2 mins· ·
LeetCode Hard Sliding-Window Hash-Table Binary-Search 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: hard First Attempt: 2026-01-14

  • Submissions: TLE → WA → RE → AC → WA → AC

Approach
#

The final array is [x, x + 1, ..., x + n - 1]. Original values inside that range, without duplicates, can stay; everything else changes.

So find the length-n value range [x, x + n - 1] containing the most distinct original values.

  1. Deduplicate and sort.
  2. Sliding window: the right end is vals[right]; shrink the left until vals[right] - vals[left] <= n - 1. Everything in the window can stay.
  3. The answer is n - most values kept.

The range can be assumed to start at an original value (pushing it right to the first original value is never worse), so starting from each vals[left] is enough.

My Attempts
#

VersionProblem
Attempt 1, TLESlid over the whole value range 0..max, testing in nums on a list, with values up to 10^9
Attempt 2, WASlid over the sorted array but did not deduplicate, so repeated values counted as “kept”. For [1, 1] one must become 2, so the answer is 1; it returned 0
Attempt 3, REAdded duplicate counting with a misspelled variable (deplicate)
Attempt 4, ACFixed the spelling; duplicate subtracted repeats inside the window
Attempt 5, WASimplifying, I dropped the deduplication again
Attempt 6, ACUsed sorted(set(nums)) directly, the cleanest

Solution
#

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        n = len(nums)
        vals = sorted(set(nums))                   # each value can be kept only once
        left = best = 0
        for right, x in enumerate(vals):
            while x - vals[left] > n - 1:
                left += 1
            best = max(best, right - left + 1)     # all of these can stay
        return n - best

The window width uses the original n, not the deduplicated length.

Complexity
#

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

Mistakes to Avoid
#

  • Skipping deduplication: a repeated value can be kept only once.
  • Using the deduplicated length for the width: the final array still has length n.

Interview Explanation
#

The result is n consecutive integers, and original values inside that range without repeats can stay. So I deduplicate and sort, then slide a window for the most values with max minus min at most n - 1, and the answer is n minus that. The width uses the original n. Time O(n log n).

Related

LeetCode 2106: Maximum Fruits Harvested After at Most K Steps
·2 mins
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search Array
LeetCode 2106 note: the harvest is always a contiguous block of fruits; covering [L, R] takes (R - L) + min(|start - L|, |R - start|) steps, so slide a window over the fruits array
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 2781: Length of the Longest Valid Substring
·3 mins
LeetCode Hard Sliding-Window Hash-Table String Trie
LeetCode 2781 note: forbidden words are at most 10 long, so each right end checks only the last 10 suffixes; on a match push the left end past it, using the shortest match (the rightmost start)
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 220: Contains Duplicate III
·3 mins
LeetCode Hard Sliding-Window Bucket-Sort Ordered-Set Sorting Array
LeetCode 220 note: with bucket size valueDiff + 1, two numbers in one bucket always qualify, so only the two neighboring buckets need checking; the window keeps the latest indexDiff numbers, so each bucket holds at most one
LeetCode 2953: Count Complete Substrings
·3 mins
LeetCode Hard Sliding-Window Hash-Table String
LeetCode 2953 note: split where neighboring letters differ by more than 2; in each segment enumerate the number of distinct letters d, slide a fixed window of d × k, and track how many letters appear exactly k times