[LeetCode 1674] Minimum Moves to Make Array Complementary
You are given an integer array nums
of even length n
and an integer limit
. In one move, you can replace any integer from nums
with another integer between 1
and limit
, inclusive.
The array nums
is complementary if for all indices i
(0-indexed), nums[i] + nums[n - 1 - i]
equals the same number. For example, the array [1,2,3,4]
is complementary because for all indices i
, nums[i] + nums[n - 1 - i] = 5
.
Return the minimum number of moves required to make nums
complementary.
Example 1:
Input: nums = [1,2,4,3], limit = 4
Output: 1
Explanation: In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed).
nums[0] + nums[3] = 1 + 3 = 4.
nums[1] + nums[2] = 2 + 2 = 4.
nums[2] + nums[1] = 2 + 2 = 4.
nums[3] + nums[0] = 3 + 1 = 4.
Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
Example 2:
Input: nums = [1,2,2,1], limit = 2
Output: 2
Explanation: In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 > limit.
Example 3:
Input: nums = [1,2,1,2], limit = 2
Output: 0
Explanation: nums is already complementary.
Constraints:
n == nums.length
2 <= n <= 105
1 <= nums[i] <= limit <= 105
n
is even.
If we fix a target sum, then it is easy to do a linear scan compute the needed operations. The possible target sum is in range [2, limit * 2], so we can check each candidate and get the best answer. The hard part is to do this efficiently since the brute force solution takes O(N * Limit), which will give TLE.
We know that the maximum operations we need is the length of the input array N if we replace every number. For each pair (A[i], A[N - 1 -i]), depending on the target sum T, we may need fewer operations.
T < 1 + Min(A[i], A[N - 1 - i]), need 2 operations;
T >= 1 + Min(A[i], A[N - 1 - i]) && T < A[i] + A[N - 1 - i], need 1 operations;
T == A[i] + A[N - 1 - i], need 0 operations;
T > A[i] + A[N - 1 - i] && T <= Max(A[i], A[N - 1 - i]) + Limit, need 1 operations;
T > Max(A[i], A[N - 1 - i]) + Limit, need 2 operations;
Instead of directly counting the needed operations for each fixed target sum T, we can save the operation count differences in a prefix sum array ps. ps[i]++ represents starting from target sum i, one pair needs 1 more operation; ps[i]-- represents 1 fewer operation. After processing each pair's such info, we then do a prefix sum computation on ps. After this, the sum value at target sum i represents the total operation difference over all pairs if coverting each pair to be sum of i. A linear scan over all possible target sums give the minimum operations.
class Solution { public int minMoves(int[] nums, int limit) { int n = nums.length; int[] ps = new int[limit * 2 + 2]; for(int i = 0; i < n / 2; i++) { int v1 = nums[i], v2 = nums[n - 1 - i]; ps[1 + Math.min(v1, v2)]--; ps[v1 + v2]--; ps[v1 + v2 + 1]++; ps[Math.max(v1, v2) + limit + 1]++; } int ans = n, curr = n; for(int i = 2; i <= limit * 2; i++) { curr += ps[i]; ans = Math.min(ans, curr); } return ans; } }
Related Problems