Skip to content

Commit 044aca2

Browse files
authored
Update 1674-minimum-moves-to-make-array-complementary.js
1 parent 1180f9e commit 044aca2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,36 @@ const minMoves = function (nums, limit) {
8484
return res
8585
}
8686

87+
// another
88+
89+
/**
90+
* @param {number[]} nums
91+
* @param {number} limit
92+
* @return {number}
93+
*/
94+
const minMoves = function (nums, limit) {
95+
const n = nums.length, { min, max } = Math
96+
const arr = Array(2 * limit + 2).fill(0)
97+
for(let i = 0, r = n / 2; i < r; i++) {
98+
const a = nums[i], b = nums[n - 1 - i]
99+
// [2, 2 * limit]
100+
arr[2] += 2
101+
arr[2 * limit + 1] -= 2
102+
// [min(a, b) + 1, max(a, b) + limit]
103+
arr[min(a, b) + 1]--
104+
arr[max(a, b) + limit + 1]++
105+
// a + b
106+
arr[a + b]--
107+
arr[a + b + 1]++
108+
109+
}
110+
let res = Infinity, cur = 0
111+
for(let i = 2, r = 2 * limit; i <= r; i++) {
112+
cur += arr[i]
113+
res = min(res, cur)
114+
}
115+
116+
return res
117+
}
118+
119+

0 commit comments

Comments
 (0)