Skip to content

Commit 1180f9e

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

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,31 @@ const minMoves = function (nums, limit) {
5656

5757
return res
5858
}
59+
60+
// another
61+
62+
/**
63+
* @param {number[]} nums
64+
* @param {number} limit
65+
* @return {number}
66+
*/
67+
const minMoves = function (nums, limit) {
68+
const n = nums.length, { min, max } = Math
69+
const arr = Array(2 * limit + 2).fill(0)
70+
for(let i = 0, r = n / 2; i < r; i++) {
71+
const a = nums[i], b = nums[n - 1 - i]
72+
arr[2] += 2
73+
arr[min(a, b) + 1]--
74+
arr[a + b]--
75+
arr[a + b + 1]++
76+
arr[max(a, b) + limit + 1]++
77+
}
78+
let res = Infinity, cur = 0
79+
for(let i = 2, r = 2 * limit; i <= r; i++) {
80+
cur += arr[i]
81+
res = min(res, cur)
82+
}
83+
84+
return res
85+
}
86+

0 commit comments

Comments
 (0)