NeetCode #257LC-2009HardSliding Window
← Back to All Problems#257 · #2009 · Minimum Number of Operations to Make Array Continuous(使数组连续的最少操作数)
📌 Problem Statement & Constraints
Given an integer array
nums, you may increment or decrement elements arbitrarily. The array is continuous if all elements are distinct and the maximum minus the minimum equals n - 1. Return the minimum number of operations to make nums continuous. Constraints: 1 <= nums.length <= 10^5, 1 <= nums[i] <= 10^9.💡 Core Algorithmic Approaches
- A continuous array is exactly a set of
ndistinct consecutive integers. - Sort and deduplicate. Then find the longest window of distinct values satisfying
max - min <= n - 1. - Every value outside that window must be changed, so the answer is
n - (longest window length). - Deduplication is essential: duplicates cannot be part of a valid continuous array, and the window is over distinct values.
💻 Benchmark Python3 Implementation
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
uniq = sorted(set(nums)) # dedupe first
l = 0
best = 0
for r in range(len(uniq)):
# the values in the window must fit in a span of n - 1
while uniq[r] - uniq[l] > n - 1:
l += 1
best = max(best, r - l + 1)
return n - best⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log n): the sort dominates; the window scan is O(n).
💾 Space Complexity
O(n) for the deduplicated array.
⚠️ Interview Pitfalls & Follow-ups
- Forgetting to deduplicate: duplicates would inflate the window length and produce an answer that is too small.
- Using
> ninstead of> n - 1:ndistinct consecutive integers spann - 1. - Sorting the original array and keeping duplicates: the window must be over distinct values.
- Trying to make the array exactly
[min, min+1, ...]: the target range can start anywhere, which is why the window slides.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Sliding Window Problems
#219 · #219Contains Duplicate II#220 · #121Best Time to Buy and Sell Stock#221 · #2379Minimum Recolors to Get K Consecutive Black Blocks#222 · #1984Minimum Difference Between Highest and Lowest of K Scores#223 · #1343Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold#224 · #1052Grumpy Bookstore Owner
中文速览与面试要点
使数组连续的最少操作数
给你一个整数数组 nums。每一次操作中,你可以将 nums 中 任意 一个元素替换成 任意 整数。如果 nums 满足以下条件,那么它是 连续 的: nums 中所有元素都 互不相同 ; nums 中 最大 元素和 最小 元素的差等于 nums.length − 1 。请你返回使 nums 连续 的最少操作次数。约束:1 ≤ nums.length ≤ 10^5;1 ≤ nums[i] ≤ 10^9。
核心要点
- 问题转化 :最终数组必须是「n 个连续整数」。我们要 尽可能多地保留原数组中的元素 (不改动),其余元素替换即可。
- 去重 + 排序(关键) :最终数组元素互不相同,因此重复的元素最多只能保留一个。先去重再排序得到数组 a。
- 滑动窗口找最长可保留段 :在 a 上找最长的连续段,使得 a[j] − a[i] <= n − 1 。该段内的元素可以直接保留(它们能嵌入某个长度为 n 的连续区间)。
- 答案 = n − 最长可保留段长度 。
常见陷阱
- 不去重 :重复元素无法同时保留在「互不相同的连续数组」中,必须先去重。
- 窗口条件写成 a[j] − a[i] <= n :应为 <= n − 1 (连续 n 个整数的极差恰好是 n−1)。
- 在「保留段」中允许重复元素 :去重后每个元素只出现一次,因此段内元素天然互不相同。
- j 指针每轮重置 :j 应单调前进(因为 i 递增时 a[i] 增大,窗口右边界不会回退),重置会导致 O(n²)。
- 误以为要构造最终数组 :题目只问操作次数,等于 n − 可保留的最大元素数 。
参考实现
class Solution:
def minOperations(self, nums: list[int]) -> int:
n = len(nums)
a = sorted(set(nums)) # 去重 + 排序
best = 0
j = 0
for i in range(len(a)):
# 找最大的 j 使 a[j] - a[i] <= n - 1
while j < len(a) and a[j] - a[i] <= n - 1:
j += 1
if j - i > best:
best = j - i
return n - best