Skip to content

Commit c8f5cac

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

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

2009-minimum-number-of-operations-to-make-array-continuous.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ const minOperations = function(nums) {
1212
}
1313

1414
let j = 0;
15-
let ans = N;
15+
let res = N;
1616
for(let i = 0; i < M; i++) {
17+
// let `j` point to the first element that is out of range -- `> nums[i] + N - 1`.
1718
while(j < M && nums[j] <= N + nums[i] - 1) j++;
18-
ans = Math.min(ans, N - (j - i));
19+
// The length of this subarray is `j - i`.
20+
// We need to replace `N - (j - i)` elements to make it continuous.
21+
res = Math.min(res, N - (j - i));
1922
}
2023

21-
return ans;
24+
return res;
2225
};

0 commit comments

Comments
 (0)