Skip to content

Commit 8e307d1

Browse files
authored
Create 2009-minimum-number-of-operations-to-make-array-continuous.js
1 parent eac6049 commit 8e307d1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const minOperations = function(nums) {
6+
const N = nums.length;
7+
if(N === 1) return 0;
8+
nums.sort((a, b) => a - b)
9+
let M = 1;
10+
for(let i = 1; i < N; i++) {
11+
if(nums[i] != nums[i-1]) nums[M++] = nums[i];
12+
}
13+
14+
let j = 0;
15+
let ans = N;
16+
for(let i = 0; i < M; i++) {
17+
while(j < M && nums[j] <= N + nums[i] - 1) j++;
18+
ans = Math.min(ans, N - (j - i));
19+
}
20+
21+
return ans;
22+
};

0 commit comments

Comments
 (0)