Skip to content

Commit d0d2521

Browse files
committed
feat: solve NO.2009
1 parent 1984052 commit d0d2521

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# 2009. Minimum Number of Operations to Make Array Continuous
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Binary Search.
5+
- Similar Questions: Longest Repeating Character Replacement, Continuous Subarray Sum, Moving Stones Until Consecutive II, Minimum One Bit Operations to Make Integers Zero, Minimum Adjacent Swaps for K Consecutive Ones.
6+
7+
## Problem
8+
9+
You are given an integer array `nums`. In one operation, you can replace **any** element in `nums` with **any** integer.
10+
11+
`nums` is considered **continuous** if both of the following conditions are fulfilled:
12+
13+
14+
15+
- All elements in `nums` are **unique**.
16+
17+
- The difference between the **maximum** element and the **minimum** element in `nums` equals `nums.length - 1`.
18+
19+
20+
For example, `nums = [4, 2, 5, 3]` is **continuous**, but `nums = [1, 2, 3, 5, 6]` is **not continuous**.
21+
22+
Return **the **minimum** number of operations to make **`nums`** ******continuous****.
23+
24+
 
25+
Example 1:
26+
27+
```
28+
Input: nums = [4,2,5,3]
29+
Output: 0
30+
Explanation: nums is already continuous.
31+
```
32+
33+
Example 2:
34+
35+
```
36+
Input: nums = [1,2,3,5,6]
37+
Output: 1
38+
Explanation: One possible solution is to change the last element to 4.
39+
The resulting array is [1,2,3,5,4], which is continuous.
40+
```
41+
42+
Example 3:
43+
44+
```
45+
Input: nums = [1,10,100,1000]
46+
Output: 3
47+
Explanation: One possible solution is to:
48+
- Change the second element to 2.
49+
- Change the third element to 3.
50+
- Change the fourth element to 4.
51+
The resulting array is [1,2,3,4], which is continuous.
52+
```
53+
54+
 
55+
**Constraints:**
56+
57+
58+
59+
- `1 <= nums.length <= 105`
60+
61+
- `1 <= nums[i] <= 109`
62+
63+
64+
65+
## Solution
66+
67+
```javascript
68+
/**
69+
* @param {number[]} nums
70+
* @return {number}
71+
*/
72+
var minOperations = function(nums) {
73+
nums.sort((a, b) => a - b);
74+
75+
var uniqueNums = [];
76+
for (var i = 0; i < nums.length; i++) {
77+
if (nums[i] !== nums[i - 1]) uniqueNums.push(nums[i]);
78+
}
79+
80+
var left = 0;
81+
var maxUniqueNumsInRange = 0;
82+
for (var right = 0; right < uniqueNums.length; right++) {
83+
while (uniqueNums[right] - uniqueNums[left] >= nums.length) left++;
84+
maxUniqueNumsInRange = Math.max(maxUniqueNumsInRange, right - left + 1);
85+
}
86+
87+
return nums.length - maxUniqueNumsInRange;
88+
};
89+
```
90+
91+
**Explain:**
92+
93+
nope.
94+
95+
**Complexity:**
96+
97+
* Time complexity : O(n * log(n)).
98+
* Space complexity : O(n).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy