Skip to content

Commit ef6f8ee

Browse files
committed
feat: add solution 2009. Minimum Number of Operations to Make Array Continuous
1 parent 943ab4d commit ef6f8ee

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2009. Minimum Number of Operations to Make Array Continuous](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-continuous)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
6+
7+
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
8+
9+
<ul>
10+
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
11+
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
12+
</ul>
13+
14+
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
15+
16+
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre><strong>Input:</strong> nums = [4,2,5,3]
22+
<strong>Output:</strong> 0
23+
<strong>Explanation:</strong>&nbsp;nums is already continuous.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre><strong>Input:</strong> nums = [1,2,3,5,6]
29+
<strong>Output:</strong> 1
30+
<strong>Explanation:</strong>&nbsp;One possible solution is to change the last element to 4.
31+
The resulting array is [1,2,3,5,4], which is continuous.
32+
</pre>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<pre><strong>Input:</strong> nums = [1,10,100,1000]
37+
<strong>Output:</strong> 3
38+
<strong>Explanation:</strong>&nbsp;One possible solution is to:
39+
- Change the second element to 2.
40+
- Change the third element to 3.
41+
- Change the fourth element to 4.
42+
The resulting array is [1,2,3,4], which is continuous.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
51+
</ul>
52+
</div>
53+
54+
<p>&nbsp;</p>
55+
56+
## Solutions
57+
58+
**Solution: `Sliding Window`**
59+
60+
- Time complexity: <em>O(nlogn)</em>
61+
- Space complexity: <em>O(n)</em>
62+
63+
<p>&nbsp;</p>
64+
65+
### **JavaScript**
66+
67+
```js
68+
/**
69+
* @param {number[]} nums
70+
* @return {number}
71+
*/
72+
const minOperations = function (nums) {
73+
const n = nums.length;
74+
const uniqueNums = [...new Set(nums)];
75+
let left = 0;
76+
77+
uniqueNums.sort((a, b) => a - b);
78+
79+
for (let index = 0; index < uniqueNums.length; index++) {
80+
if (uniqueNums[index] - uniqueNums[left] > n - 1) {
81+
left += 1;
82+
}
83+
}
84+
85+
return n - (uniqueNums.length - left);
86+
};
87+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const minOperations = function (nums) {
6+
const n = nums.length;
7+
const uniqueNums = [...new Set(nums)];
8+
let left = 0;
9+
10+
uniqueNums.sort((a, b) => a - b);
11+
12+
for (let index = 0; index < uniqueNums.length; index++) {
13+
if (uniqueNums[index] - uniqueNums[left] > n - 1) {
14+
left += 1;
15+
}
16+
}
17+
18+
return n - (uniqueNums.length - left);
19+
};

0 commit comments

Comments
 (0)