Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 48c9a34

Browse files
committed
Add solution #2009
1 parent 07de3a7 commit 48c9a34

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,685 LeetCode solutions in JavaScript
1+
# 1,686 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1541,6 +1541,7 @@
15411541
2006|[Count Number of Pairs With Absolute Difference K](./solutions/2006-count-number-of-pairs-with-absolute-difference-k.js)|Easy|
15421542
2007|[Find Original Array From Doubled Array](./solutions/2007-find-original-array-from-doubled-array.js)|Medium|
15431543
2008|[Maximum Earnings From Taxi](./solutions/2008-maximum-earnings-from-taxi.js)|Medium|
1544+
2009|[Minimum Number of Operations to Make Array Continuous](./solutions/2009-minimum-number-of-operations-to-make-array-continuous.js)|Hard|
15441545
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15451546
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15461547
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 2009. Minimum Number of Operations to Make Array Continuous
3+
* https://leetcode.com/problems/minimum-number-of-operations-to-make-array-continuous/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums. In one operation, you can replace any element in nums
7+
* with any integer.
8+
*
9+
* nums is considered continuous if both of the following conditions are fulfilled:
10+
* - All elements in nums are unique.
11+
* - The difference between the maximum element and the minimum element in nums equals
12+
* nums.length - 1.
13+
*
14+
* For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.
15+
*
16+
* Return the minimum number of operations to make nums continuous.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {number}
22+
*/
23+
var minOperations = function(nums) {
24+
const n = nums.length;
25+
const uniqueNums = [...new Set(nums)].sort((a, b) => a - b);
26+
let result = n;
27+
28+
for (let i = 0, j = 0; i < uniqueNums.length; i++) {
29+
while (j < uniqueNums.length && uniqueNums[j] - uniqueNums[i] <= n - 1) {
30+
j++;
31+
}
32+
result = Math.min(result, n - (j - i));
33+
}
34+
35+
return result;
36+
};

0 commit comments

Comments
 (0)