★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-znuijwrb-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We have two integer sequences A
and B
of the same non-zero length.git
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.github
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:spa
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
和 B
。code
咱們能夠交換 A[i]
和 B[i]
的元素。注意這兩個元素在各自的序列中應該處於相同的位置。htm
在交換過一些元素以後,數組 A
和 B
都應該是嚴格遞增的(數組嚴格遞增的條件僅爲A[0] < A[1] < A[2] < ... < A[A.length - 1]
)。blog
給定數組 A
和 B
,請返回使得兩個數組均保持嚴格遞增狀態的最小交換次數。假設給定的輸入老是有效的。element
示例: 輸入: A = [1,3,5,4], B = [1,2,3,7] 輸出: 1 解釋: 交換 A[3] 和 B[3] 後,兩個數組以下: A = [1, 3, 5, 7] , B = [1, 2, 3, 4] 兩個數組均爲嚴格遞增的。
注意:
A, B
兩個數組的長度老是相等的,且長度的範圍爲 [1, 1000]
。A[i], B[i]
均爲 [0, 2000]
區間內的整數。1 class Solution { 2 func minSwap(_ A: [Int], _ B: [Int]) -> Int { 3 var n1:Int = 0 4 var s1:Int = 1 5 var n:Int = A.count 6 for i in 1..<n 7 { 8 var n2:Int = Int.max 9 var s2:Int = Int.max 10 if A[i - 1] < A[i] && B[i - 1] < B[i] 11 { 12 n2 = min(n2, n1) 13 s2 = min(s2, s1 + 1) 14 } 15 if A[i - 1] < B[i] && B[i - 1] < A[i] 16 { 17 n2 = min(n2, s1) 18 s2 = min(s2, n1 + 1) 19 } 20 n1 = n2 21 s1 = s2 22 } 23 return min(n1, s1) 24 } 25 }
76ms
1 class Solution { 2 func minSwap(_ A: [Int], _ B: [Int]) -> Int { 3 var swapRecord = 1, fixRecord = 0 4 for i in 1..<A.count { 5 if A[i-1] >= B[i] || B[i-1] >= A[i] { 6 swapRecord += 1 7 } 8 else if A[i - 1] >= A[i] || B[i - 1] >= B[i] { 9 let temp = swapRecord 10 swapRecord = fixRecord + 1 11 fixRecord = temp 12 } 13 else { 14 let minv = min(swapRecord, fixRecord) 15 swapRecord = minv + 1 16 fixRecord = minv 17 } 18 } 19 return min(swapRecord, fixRecord) 20 } 21 22 func minSwapDP(_ A: [Int], _ B: [Int], _ i: Int, _ sum: Int) -> Int { 23 24 if i >= A.count { 25 return sum 26 } 27 28 if i == 0 { return minSwapDP(A, B, i+1, sum) } 29 30 if A[i] > A[i-1] && B[i] > B[i-1] { 31 return minSwapDP(A, B, i+1, sum) 32 } 33 34 var canSwapPre = (A[i-1] < B[i] && B[i-1] < A[i]) 35 if canSwapPre { 36 for k in 0..<i-1 { 37 if A[i-1] <= B[k] || B[i-1] <= A[k] { 38 canSwapPre = false 39 break 40 } 41 } 42 } 43 let canSwapCur = (B[i] > A[i-1] && A[i] > B[i-1]) 44 45 var A2 = A, B2 = B 46 let tmp2 = A2[i-1] 47 A2[i-1] = B2[i-1] 48 B2[i-1] = tmp2 49 50 var A1 = A, B1 = B 51 let tmp = A1[i] 52 A1[i] = B1[i] 53 B1[i] = tmp 54 if canSwapPre && canSwapCur { 55 return min(minSwapDP(A2, B2, i, sum+1), minSwapDP(A1, B1, i+1, sum+1)) 56 } 57 else if canSwapCur { 58 return minSwapDP(A1, B1, i+1, sum+1) 59 } 60 else { 61 return minSwapDP(A2, B2, i, sum+1) 62 } 63 } 64 }