|
| 1 | +--- |
| 2 | +id: minimum-number-of-operations-to-make-array-continuous |
| 3 | +title: Minimum Number of Operations to Make Array Continuous |
| 4 | +sidebar_label: 2009 Minimum Number of Operations to Make Array Continuous |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Sorting |
| 8 | + - Sliding Window |
| 9 | + - LeetCode |
| 10 | +description: "This is a solution to the Minimum Number of Operations to Make Array Continuous problem on LeetCode." |
| 11 | +sidebar_position: 2009 |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +You are given an integer array `nums`. In one operation, you can replace any element in `nums` with any integer. |
| 17 | + |
| 18 | +`nums` is considered continuous if both of the following conditions are fulfilled: |
| 19 | + |
| 20 | +1. All elements in `nums` are unique. |
| 21 | +2. The difference between the maximum element and the minimum element in `nums` equals `nums.length - 1`. |
| 22 | + |
| 23 | +For example, `nums = [4, 2, 5, 3]` is continuous, but `nums = [1, 2, 3, 5, 6]` is not continuous. |
| 24 | + |
| 25 | +Return the minimum number of operations to make `nums` continuous. |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: nums = [4,2,5,3] |
| 33 | +Output: 0 |
| 34 | +Explanation: nums is already continuous. |
| 35 | +``` |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +``` |
| 40 | +Input: nums = [1,2,3,5,6] |
| 41 | +Output: 1 |
| 42 | +Explanation: One possible solution is to change the last element to 4. |
| 43 | +The resulting array is [1,2,3,5,4], which is continuous. |
| 44 | +``` |
| 45 | + |
| 46 | +**Example 3:** |
| 47 | + |
| 48 | +``` |
| 49 | +Input: nums = [1,10,100,1000] |
| 50 | +Output: 3 |
| 51 | +Explanation: One possible solution is to: |
| 52 | +
|
| 53 | +Change the second element to 2. |
| 54 | +Change the third element to 3. |
| 55 | +Change the fourth element to 4. |
| 56 | +The resulting array is [1,2,3,4], which is continuous. |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | +### Constraints |
| 61 | + |
| 62 | +- `1 <= nums.length <= 10^5` |
| 63 | +- `1 <= nums[i] <= 10^9` |
| 64 | + |
| 65 | +### Approach |
| 66 | + |
| 67 | +To solve this problem: |
| 68 | +1. Sort the array `nums` to facilitate finding the longest continuous subsequence. |
| 69 | +2. Use a sliding window to find the longest subsequence that satisfies the conditions of being continuous. |
| 70 | +3. Calculate the minimum number of operations as the difference between the length of `nums` and the length of the longest continuous subsequence found. |
| 71 | + |
| 72 | +#### C++ Solution |
| 73 | + |
| 74 | +```cpp |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + int minOperations(vector<int>& nums) { |
| 78 | + sort(nums.begin(), nums.end()); |
| 79 | + nums.erase(unique(nums.begin(), nums.end()), nums.end()); |
| 80 | + |
| 81 | + int n = nums.size(); |
| 82 | + int ans = n; |
| 83 | + for (int i = 0, j = 0; i < n; ++i) { |
| 84 | + while (j < n && nums[j] <= nums[i] + n - 1) { |
| 85 | + ++j; |
| 86 | + } |
| 87 | + ans = min(ans, n - (j - i)); |
| 88 | + } |
| 89 | + |
| 90 | + return ans; |
| 91 | + } |
| 92 | +}; |
| 93 | +``` |
0 commit comments