801. Minimum Swaps To Make Sequences Increasing 使序列递增的最小交换次数

@TOC

# 题目描述

We have two integer sequences `A` and `B` of the same non-zero length.

We are allowed to swap elements `A[i]` and `B[i]`. Note that both elements are in the same index position in their respective sequences.

At the end of some number of swaps, `A` and `B` are both strictly increasing. (A sequence is strictly increasing if and only if `A[0] < A[1] < A[2] < ... < A[A.length - 1]`.)

Given A and B, return the minimum number of swaps to make both sequences strictly increasing. It is guaranteed that the given input always makes it possible.

Example:

``````Input: A = [1,3,5,4], B = [1,2,3,7]
Output: 1
Explanation:
Swap A[3] and B[3].  Then the sequences are:
A = [1, 3, 5, 7] and B = [1, 2, 3, 4]
which are both strictly increasing.
``````

Note:

• `A`, `B` are arrays with the same length, and that length will be in the range `[1, 1000]`.
• `A[i]`, `B[i]` are integer values in the range `[0, 2000]`.

# 解题方法

# 动态规划

`A[i] > B[i - 1] and B[i] > A[i - 1]`时，我们如果不交换当前的数字，同时对前面的位置强制交换，判断交换后的次数是不是比当前的交换次数少；如果我们交换这个位置，同时强制前面的数字不交换，那么当前的交换次数应该是前面不交换的次数+1和当前交换次数的最小值。

``````class Solution(object):
def minSwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
N = len(A)
keep = [float('inf')] * N
swap = [float('inf')] * N
keep[0] = 0
swap[0] = 1
for i in range(1, N):
if A[i] > A[i - 1] and B[i] > B[i - 1]:
keep[i] = keep[i - 1]
swap[i] = swap[i - 1] + 1
if A[i] > B[i - 1] and B[i] > A[i - 1]:
keep[i] = min(keep[i], swap[i - 1])
swap[i] = min(swap[i], keep[i - 1] + 1)
return min(keep[N - 1], swap[N - 1])
``````

``````class Solution(object):
def minSwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
N = len(A)
dp = [[float('inf'), float('inf')] for _ in range(N)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(1, N):
if A[i] > A[i - 1] and B[i] > B[i - 1]:
dp[i][0] = dp[i - 1][0]
dp[i][1] = dp[i - 1][1] + 1
if A[i] > B[i - 1] and B[i] > A[i - 1]:
dp[i][0] = min(dp[i][0], dp[i - 1][1])
dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1)
return min(dp[N - 1][0], dp[N - 1][1])
``````

``````class Solution(object):
def minSwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
N = len(A)
keep, swap = 0, 1
for i in range(1, N):
curswap, curkeep = float('inf'), float('inf')
if A[i] > A[i - 1] and B[i] > B[i - 1]:
curkeep, curswap = keep, swap + 1
if A[i] > B[i - 1] and B[i] > A[i - 1]:
curkeep, curswap = min(curkeep, swap), min(curswap, keep + 1)
keep, swap = curkeep, curswap
return min(keep, swap)
``````

# 参考资料

https://leetcode.com/problems/flip-string-to-monotone-increasing/discuss/183859/Java-DP-using-O(N)-time-and-O(1)-space

# 日期

2018 年 10 月 21 日 —— 新的一周又开始了