LeetCode 题解工作台

使序列递增的最小交换次数

我们有两个长度相等且不为空的整型数组 nums1 和 nums2 。在一次操作中,我们可以交换 nums1[i] 和 nums2[i] 的元素。 例如,如果 nums1 = [1,2,3, 8 ] , nums2 =[5,6,7, 4 ] ,你可以交换 i = 3 处的元素,得到 nums1 =[1…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

定义 , 分别表示使得下标 的元素序列严格递增,且第 个元素不交换、交换的最小交换次数。下标从 开始。 当 时,有 $a = 0$, 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

我们有两个长度相等且不为空的整型数组 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]

返回 使 nums1nums2 严格递增 所需操作的最小次数

数组 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 <= 105
  • nums2.length == nums1.length
  • 0 <= nums1[i], nums2[i] <= 2 * 105
lightbulb

解题思路

方法一:动态规划

定义 aa, bb 分别表示使得下标 [0..i][0..i] 的元素序列严格递增,且第 ii 个元素不交换、交换的最小交换次数。下标从 00 开始。

i=0i=0 时,有 a=0a = 0, b=1b=1

i>0i\gt 0 时,我们先将此前 aa, bb 的值保存在 xx, yy 中,然后分情况讨论:

如果 nums1[i1]nums1[i]nums1[i - 1] \ge nums1[i] 或者 nums2[i1]nums2[i]nums2[i - 1] \ge nums2[i],为了使得两个序列均严格递增,下标 i1i-1ii 对应的元素的相对位置必须发生变化。也就是说,如果前一个位置交换了,那么当前位置不交换,因此有 a=ya = y;如果前一个位置没有交换,那么当前位置必须交换,因此有 b=x+1b = x + 1

否则,下标 i1i-1ii 对应的元素的相对位置可以不发生变化,那么有 b=y+1b = y + 1。另外,如果满足 nums1[i1]<nums2[i]nums1[i - 1] \lt nums2[i] 并且 nums2[i1]<nums1[i]nums2[i - 1] \lt nums1[i],那么下标 i1i-1ii 对应的元素的相对位置可以发生变化,此时 aabb 可以取较小值,因此有 a=min(a,y)a = \min(a, y)b=min(b,x+1)b = \min(b, x + 1)

最后,返回 aabb 中较小值即可。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
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)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

使序列递增的最小交换次数题解:状态·转移·动态规划 | LeetCode #801 困难