3724. 转换数组的最少操作次数
October 30, 2025 · View on GitHub
题目描述
给你两个整数数组,第一个数组 nums1 长度为 n,以及第二个数组 nums2 长度为 n + 1。
你的目标是使用 最少 的操作次数将 nums1 转换为 nums2。
你可以执行以下操作 任意 次,每次选择一个下标 i:
- 将
nums1[i]增加 1。 - 将
nums1[i]减少 1。 - 将
nums1[i]追加 到数组的 末尾 。
返回将 nums1 转换为 nums2 所需的 最少 操作次数。
示例 1:
输入: nums1 = [2,8], nums2 = [1,7,3]
输出: 4
解释:
| 步骤 | i |
操作 | nums1[i] |
更新后的 nums1 |
|---|---|---|---|---|
| 1 | 0 | 追加 | - | [2, 8, 2] |
| 2 | 0 | 减少 | 减少到 1 | [1, 8, 2] |
| 3 | 1 | 减少 | 减少到 7 | [1, 7, 2] |
| 4 | 2 | 增加 | 增加到 3 | [1, 7, 3] |
因此,经过 4 次操作后,nums1 转换为 nums2。
示例 2:
输入: nums1 = [1,3,6], nums2 = [2,4,5,3]
输出: 4
解释:
| 步骤 | i |
操作 | nums1[i] |
更新后的 nums1 |
|---|---|---|---|---|
| 1 | 1 | 追加 | - | [1, 3, 6, 3] |
| 2 | 0 | 增加 | 增加到 2 | [2, 3, 6, 3] |
| 3 | 1 | 增加 | 增加到 4 | [2, 4, 6, 3] |
| 4 | 2 | 减少 | 减少到 5 | [2, 4, 5, 3] |
因此,经过 4 次操作后,nums1 转换为 nums2。
示例 3:
输入: nums1 = [2], nums2 = [3,4]
输出: 3
解释:
| 步骤 | i |
操作 | nums1[i] |
更新后的 nums1 |
|---|---|---|---|---|
| 1 | 0 | 增加 | 增加到 3 | [3] |
| 2 | 0 | 追加 | - | [3, 3] |
| 3 | 1 | 增加 | 增加到 4 | [3, 4] |
因此,经过 3 次操作后,nums1 转换为 nums2。
提示:
1 <= n == nums1.length <= 105nums2.length == n + 11 <= nums1[i], nums2[i] <= 105
解法
方法一:贪心
我们定义一个答案变量 来记录最少操作次数,初始值为 $1$,表示将最后一个元素追加到数组末尾所需的操作次数。
然后我们遍历数组的前 个元素,对于每一对对应的元素 ,我们计算它们之间的差值,并将其加入到 中。
在遍历过程中,我们还需要检查 是否成立。如果成立,说明我们可以通过调整 来直接达到 ,否则我们需要记录一个最小的差值 ,表示将某个元素调整到 所需的最小操作次数。
最后,如果在遍历结束后没有找到满足条件的元素,我们需要将 加入到 中,表示我们需要额外的操作来调整某个元素到 。
时间复杂度 ,其中 是数组的长度。空间复杂度 。
Python3
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
ans = 1
ok = False
d = inf
for x, y in zip(nums1, nums2):
if x < y:
x, y = y, x
ans += x - y
d = min(d, abs(x - nums2[-1]), abs(y - nums2[-1]))
ok = ok or y <= nums2[-1] <= x
if not ok:
ans += d
return ans
Java
class Solution {
public long minOperations(int[] nums1, int[] nums2) {
long ans = 1;
int n = nums1.length;
boolean ok = false;
int d = 1 << 30;
for (int i = 0; i < n; ++i) {
int x = Math.max(nums1[i], nums2[i]);
int y = Math.min(nums1[i], nums2[i]);
ans += x - y;
d = Math.min(d, Math.min(Math.abs(x - nums2[n]), Math.abs(y - nums2[n])));
ok = ok || (nums2[n] >= y && nums2[n] <= x);
}
if (!ok) {
ans += d;
}
return ans;
}
}
C++
class Solution {
public:
long long minOperations(vector<int>& nums1, vector<int>& nums2) {
long long ans = 1;
int n = nums1.size();
bool ok = false;
int d = 1 << 30;
for (int i = 0; i < n; ++i) {
int x = max(nums1[i], nums2[i]);
int y = min(nums1[i], nums2[i]);
ans += x - y;
d = min(d, min(abs(x - nums2[n]), abs(y - nums2[n])));
ok = ok || (nums2[n] >= y && nums2[n] <= x);
}
if (!ok) {
ans += d;
}
return ans;
}
};
Go
func minOperations(nums1 []int, nums2 []int) int64 {
var ans int64 = 1
n := len(nums1)
ok := false
d := 1 << 30
for i := 0; i < n; i++ {
x := max(nums1[i], nums2[i])
y := min(nums1[i], nums2[i])
ans += int64(x - y)
d = min(d, min(abs(x-nums2[n]), abs(y-nums2[n])))
if nums2[n] >= y && nums2[n] <= x {
ok = true
}
}
if !ok {
ans += int64(d)
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
TypeScript
function minOperations(nums1: number[], nums2: number[]): number {
let ans = 1;
const n = nums1.length;
let ok = false;
let d = 1 << 30;
for (let i = 0; i < n; ++i) {
const x = Math.max(nums1[i], nums2[i]);
const y = Math.min(nums1[i], nums2[i]);
ans += x - y;
d = Math.min(d, Math.abs(x - nums2[n]), Math.abs(y - nums2[n]));
if (nums2[n] >= y && nums2[n] <= x) {
ok = true;
}
}
if (!ok) {
ans += d;
}
return ans;
}