使数组互补的最少操作次数
难度:
标签:
题目描述
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.
代码结果
运行时间: 159 ms, 内存: 30.8 MB
/*
思路:
1. 使用 Java Stream 来进行统计和计算。
2. 对每一对 (nums[i], nums[n-1-i]),考虑它们的和。
3. 对每一个可能的和 nums[i] + nums[n-1-i],统计其频率。
4. 对于每一个目标和,从 2 到 2 * limit,计算出需要的最小操作次数。
*/
import java.util.stream.IntStream;
public class Solution {
public int minMoves(int[] nums, int limit) {
int n = nums.length;
int[] delta = new int[2 * limit + 2];
IntStream.range(0, n / 2).forEach(i -> {
int a = nums[i], b = nums[n - 1 - i];
delta[2] += 2;
delta[Math.min(a, b) + 1] -= 1;
delta[a + b] -= 1;
delta[a + b + 1] += 1;
delta[Math.max(a, b) + limit + 1] += 1;
});
return IntStream.range(2, 2 * limit + 1).reduce(n, (minMoves, i) -> {
return Math.min(minMoves, delta[i] += (i == 2 ? 0 : delta[i - 1]));
});
}
}
解释
方法:
此题解利用了差分数组的技巧来优化区间更新的操作。差分数组diff用于记录操作的变化:初始时假设每对(i, n-1-i)需要两次操作来达到互补状态,这是diff数组全体加2的原因。对于每对a和b(其中a是较小的那个以简化区间操作),首先减去一次操作,因为他们已经共同贡献了一个和。然后再根据可能的最小和和最大和调整diff数组,以确保最少的修改次数覆盖更多的配对。最后,通过累积和找出最少操作次数,即差分数组的最小累积值。
时间复杂度:
O(n + limit)
空间复杂度:
O(limit)
代码细节讲解
🦆
在算法中,为什么初始时`diff[1]`增加2,这是基于什么考虑?
▷🦆
请解释为什么在`diff[a+b-1]`和`diff[a+b]`之间调整差分数组,这样的处理是如何影响最终的操作次数计算的?
▷🦆
算法在处理边界条件时,如何确保不会超出`diff`数组的有效索引范围?
▷