forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-moves-to-make-array-complementary.cpp
More file actions
22 lines (21 loc) · 1.04 KB
/
minimum-moves-to-make-array-complementary.cpp
File metadata and controls
22 lines (21 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Time: O(n + k)
// Space: O(k)
class Solution {
public:
int minMoves(vector<int>& nums, int limit) {
vector<int> diff(2 * (limit + 1));
for (int i = 0; i < size(nums) / 2; ++i) {
int left = nums[i], right = nums[size(nums) - 1 - i];
--diff[min(left, right) + 1]; // if target total grows to min(left, right)+1, one less move
--diff[left + right]; // if target total grows to left+right, one less move
++diff[left + right + 1]; // if target total grows to left+right+1, one more move
++diff[max(left, right) + limit + 1]; // if target total grows to max(left, right)+limit+1, one more move
}
int result = size(nums), count = size(nums); // default is to move all nums
for (int total = 2; total <= limit * 2; ++total) { // enumerate all possible target totals
count += diff[total];
result = min(result, count);
}
return result;
}
};