LeetCode 题解工作台
使序列递增的最小交换次数
我们有两个长度相等且不为空的整型数组 nums1 和 nums2 。在一次操作中,我们可以交换 nums1[i] 和 nums2[i] 的元素。 例如,如果 nums1 = [1,2,3, 8 ] , nums2 =[5,6,7, 4 ] ,你可以交换 i = 3 处的元素,得到 nums1 =[1…
2
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
定义 , 分别表示使得下标 的元素序列严格递增,且第 个元素不交换、交换的最小交换次数。下标从 开始。 当 时,有 $a = 0$, 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
我们有两个长度相等且不为空的整型数组 nums1 和 nums2 。在一次操作中,我们可以交换 nums1[i] 和 nums2[i]的元素。
- 例如,如果
nums1 = [1,2,3,8],nums2 =[5,6,7,4],你可以交换i = 3处的元素,得到nums1 =[1,2,3,4]和nums2 =[5,6,7,8]。
返回 使 nums1 和 nums2 严格递增 所需操作的最小次数 。
数组 arr 严格递增 且 arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1] 。
注意:
- 用例保证可以实现操作。
示例 1:
输入: nums1 = [1,3,5,4], nums2 = [1,2,3,7] 输出: 1 解释: 交换 A[3] 和 B[3] 后,两个数组如下: A = [1, 3, 5, 7] , B = [1, 2, 3, 4] 两个数组均为严格递增的。
示例 2:
输入: nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9] 输出: 1
提示:
2 <= nums1.length <= 105nums2.length == nums1.length0 <= nums1[i], nums2[i] <= 2 * 105
解题思路
方法一:动态规划
定义 , 分别表示使得下标 的元素序列严格递增,且第 个元素不交换、交换的最小交换次数。下标从 开始。
当 时,有 , 。
当 时,我们先将此前 , 的值保存在 , 中,然后分情况讨论:
如果 或者 ,为了使得两个序列均严格递增,下标 和 对应的元素的相对位置必须发生变化。也就是说,如果前一个位置交换了,那么当前位置不交换,因此有 ;如果前一个位置没有交换,那么当前位置必须交换,因此有 。
否则,下标 和 对应的元素的相对位置可以不发生变化,那么有 。另外,如果满足 并且 ,那么下标 和 对应的元素的相对位置可以发生变化,此时 和 可以取较小值,因此有 和 。
最后,返回 和 中较小值即可。
时间复杂度 ,空间复杂度 。
class Solution:
def minSwap(self, nums1: List[int], nums2: List[int]) -> int:
a, b = 0, 1
for i in range(1, len(nums1)):
x, y = a, b
if nums1[i - 1] >= nums1[i] or nums2[i - 1] >= nums2[i]:
a, b = y, x + 1
else:
b = y + 1
if nums1[i - 1] < nums2[i] and nums2[i - 1] < nums1[i]:
a, b = min(a, y), min(b, x + 1)
return min(a, b)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Focus on the candidate's ability to implement dynamic programming solutions and handle state transitions efficiently.
- question_mark
Watch for how well the candidate optimizes space and time complexity in dynamic programming approaches.
- question_mark
Evaluate if the candidate can clearly explain how their solution ensures that both sequences are strictly increasing after the minimum number of swaps.
常见陷阱
外企场景- error
Not considering all valid transitions between states, leading to incorrect or suboptimal solutions.
- error
Forgetting to account for boundary conditions, such as checking the relationship between consecutive elements in both sequences.
- error
Misunderstanding the problem's constraints, such as assuming swapping elements at certain indices is always beneficial without verifying the effect on subsequent positions.
进阶变体
外企场景- arrow_right_alt
What if the problem allows for more than two arrays to be considered? This would introduce complexity in managing multiple sequences and maintaining the increasing order.
- arrow_right_alt
Consideration of other types of operations besides swapping, such as sorting or shifting elements, which would change the dynamic programming approach.
- arrow_right_alt
What if the sequence lengths are significantly larger, such as reaching up to 10^6 elements? This would require optimizing both time and space complexity to handle large inputs.