#2009
Minimum Number of Operations to Make Array Continuous
candidate master · 1365 · lc hard +32 · verified · 52.1% accepted · 1,990 likes · top 42%
Description
An array nums is continuous when all elements are distinct and the range from minimum to maximum equals nums.length - 1. In one operation you may replace any element with any integer. Return the minimum number of such operations needed to make nums continuous.
Example 1:
Input: nums = [4,2,5,3]
Output: 0
Explanation: nums is already continuous.
Example 2:
Input: nums = [1,2,3,5,6]
Output: 1
Explanation: One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
Example 3:
Input: nums = [1,10,100,1000]
Output: 3
Explanation: 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.
Code
1
2
3