Skip to content

Commit 4f3155c

Browse files
authored
Update 1674-minimum-moves-to-make-array-complementary.js
1 parent 36d3b43 commit 4f3155c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

1674-minimum-moves-to-make-array-complementary.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,31 @@ const minMoves = function (nums, limit) {
116116
return res
117117
}
118118

119+
// another
120+
121+
/**
122+
* @param {number[]} nums
123+
* @param {number} limit
124+
* @return {number}
125+
*/
126+
const minMoves = function(nums, limit) {
127+
const n = nums.length
128+
const arr = Array(2 * limit + 2).fill(0)
129+
for(let i = 0; i < n / 2; i++) {
130+
const a = nums[i], b = nums[n - 1 - i]
131+
const l = Math.min(a, b), r = Math.max(a, b)
132+
arr[l + 1]--
133+
arr[l + r]--
134+
arr[l + r + 1]++
135+
arr[r + limit + 1]++
136+
}
137+
let res = n, cur = n
138+
for(let e of arr) {
139+
cur += e
140+
res = Math.min(res, cur)
141+
}
142+
143+
return res
144+
};
145+
119146

0 commit comments

Comments
 (0)