LeetCode 2009. Minimum Number of Operations to Make Array Continuous Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2009. Minimum Number of Operations to Make Array Continuous

Description

You are given an integer array nums. In one operation, you can replace any element in nums with any integer.

nums is considered continuous if both of the following conditions are fulfilled:

  • All elements in nums are unique.
  • The difference between the maximum element and the minimum element in nums equals nums.length - 1.

For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.

Return the minimum number of operations 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.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Solution 1: Sorting + Deduplication + Binary Search

First, we sort the array and remove duplicates.

Then, we traverse the array, enumerating the current element nums[i] as the minimum value of the consecutive array. We use binary search to find the first position j that is greater than nums[i] + n - 1. Then, j-i is the length of the consecutive array when the current element is the minimum value. We update the answer, i.e., ans = min(ans, n - (j - i)).

Finally, we return ans.

The time complexity is O(n × log n), and the space complexity is O(log n). Here, n is the length of the array.

PythonJavaC++GoTypeScriptRust
class Solution: def minOperations(self, nums: List[int]) -> int: ans = n = len(nums) nums = sorted(set(nums)) for i, v in enumerate(nums): j = bisect_right(nums, v + n - 1) ans = min(ans, n - (j - i)) return ans(code-box)

Solution 2: Sorting + Deduplication + Two Pointers

Similar to Solution 1, we first sort the array and remove duplicates.

Then, we traverse the array, enumerating the current element nums[i] as the minimum value of the consecutive array. We use two pointers to find the first position j that is greater than nums[i] + n - 1. Then, j-i is the length of the consecutive array when the current element is the minimum value. We update the answer, i.e., ans = min(ans, n - (j - i)).

Finally, we return ans.

The time complexity is O(n × log n), and the space complexity is O(log n). Here, n is the length of the array.

PythonJavaC++GoTypeScriptRust
class Solution: def minOperations(self, nums: List[int]) -> int: n = len(nums) nums = sorted(set(nums)) ans, j = n, 0 for i, v in enumerate(nums): while j < len(nums) and nums[j] - v <= n - 1: j += 1 ans = min(ans, n - (j - i)) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !