-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution(python): 2009. Minimum Number of Operations to Make...
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Hard/2009. Minimum Number of Operations to Make Array Continuous/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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. |
13 changes: 13 additions & 0 deletions
13
Hard/2009. Minimum Number of Operations to Make Array Continuous/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution(object): | ||
def minOperations(self, nums): | ||
n = len(nums) | ||
nums = sorted(set(nums)) | ||
ans = sys.maxsize | ||
|
||
for i, s in enumerate(nums): | ||
e = s + n - 1 | ||
idx = bisect_right(nums, e) | ||
|
||
ans = min(ans, n - (idx - i)) | ||
return ans | ||
|