Input: nums =[1,2,3,5,6]Output: 1Explanation: One possible solution is to change the last element to 4.The resulting array is[1,2,3,5,4], which is continuous.
Input: nums =[1,10,100,1000]Output: 3Explanation: One possible solution is to:- Change the second element to 2.- Change the third element to 3.- Change the fourth element to 4.The resulting array is[1,2,3,4], which is continuous.
We want the array to be a sequence of n unique consecutive numbers. After removing duplicates and sorting, use a sliding window to find the largest possible window of length n. The minimum number of operations is n minus the size of the largest such window.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int minOperations(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
int ans = n, j =0;
for (int i =0; i < nums.size(); ++i) {
while (j < nums.size() && nums[j] - nums[i] < n) ++j;
ans = min(ans, n - (j - i));
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import"sort"funcminOperations(nums []int) int {
n:= len(nums)
sort.Ints(nums)
uniq:= []int{}
fori, x:=rangenums {
ifi==0||x!=nums[i-1] { uniq = append(uniq, x) }
}
ans, j:=n, 0fori:=0; i < len(uniq); i++ {
forj < len(uniq) &&uniq[j]-uniq[i] < n { j++ }
ifn-(j-i) < ans { ans = n- (j-i) }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
classSolution {
publicintminOperations(int[] nums) {
int n = nums.length;
Set<Integer> set =new HashSet<>();
for (int x : nums) set.add(x);
int[] arr = set.stream().mapToInt(Integer::intValue).toArray();
Arrays.sort(arr);
int ans = n, j = 0;
for (int i = 0; i < arr.length; ++i) {
while (j < arr.length&& arr[j]- arr[i]< n) ++j;
ans = Math.min(ans, n - (j - i));
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funminOperations(nums: IntArray): Int {
val n = nums.size
val arr = nums.toSet().sorted()
var ans = n
var j = 0for (i in arr.indices) {
while (j < arr.size && arr[j] - arr[i] < n) j++ ans = minOf(ans, n - (j - i))
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defminOperations(self, nums: list[int]) -> int:
n = len(nums)
arr = sorted(set(nums))
ans = n
j =0for i in range(len(arr)):
while j < len(arr) and arr[j] - arr[i] < n:
j +=1 ans = min(ans, n - (j - i))
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use std::collections::HashSet;
impl Solution {
pubfnmin_operations(nums: Vec<i32>) -> i32 {
let n = nums.len() asi32;
letmut arr: Vec<i32>= nums.into_iter().collect::<HashSet<_>>().into_iter().collect();
arr.sort();
letmut ans = n;
letmut j =0;
for i in0..arr.len() {
while j < arr.len() && arr[j] - arr[i] < n { j +=1; }
ans = ans.min(n - (j asi32- i asi32));
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
minOperations(nums: number[]):number {
constn=nums.length;
constarr= Array.from(newSet(nums)).sort((a, b) =>a-b);
letans=n, j=0;
for (leti=0; i<arr.length; ++i) {
while (j<arr.length&&arr[j] -arr[i] <n) ++j;
ans= Math.min(ans, n- (j-i));
}
returnans;
}
}