-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path801-MinimumSwapsToMakeSequencesIncreasing.go
More file actions
96 lines (85 loc) · 3.16 KB
/
801-MinimumSwapsToMakeSequencesIncreasing.go
File metadata and controls
96 lines (85 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
// 801. Minimum Swaps To Make Sequences Increasing
// You are given two integer arrays of the same length nums1 and nums2.
// In one operation, you are allowed to swap nums1[i] with nums2[i].
// For example, if nums1 = [1,2,3,8], and nums2 = [5,6,7,4],
// you can swap the element at i = 3 to obtain nums1 = [1,2,3,4] and nums2 = [5,6,7,8].
// Return the minimum number of needed operations to make nums1 and nums2 strictly increasing.
// The test cases are generated so that the given input always makes it possible.
// An array arr is strictly increasing if and only if arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1].
// Example 1:
// Input: nums1 = [1,3,5,4], nums2 = [1,2,3,7]
// Output: 1
// Explanation:
// Swap nums1[3] and nums2[3]. Then the sequences are:
// nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
// which are both strictly increasing.
// Example 2:
// Input: nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
// Output: 1
// Constraints:
// 2 <= nums1.length <= 10^5
// nums2.length == nums1.length
// 0 <= nums1[i], nums2[i] <= 2 * 10^5
import "fmt"
func minSwap(nums1 []int, nums2 []int) int {
dp := make([][]int, len(nums1))
for i, _ := range dp {
dp[i] = make([]int, 2)
dp[i][0], dp[i][1] = -1, -1
}
min := func (x, y int) int { if x < y { return x; }; return y; }
var dfs func (i, prevA, prevB, swap int) int
dfs = func (i, prevA, prevB, swap int) int {
if i == len(nums1) {
return 0
}
if dp[i][swap] != -1 {
return dp[i][swap]
}
res := 100000
if nums1[i] > prevA && nums2[i] > prevB {
res = dfs(i+1, nums1[i], nums2[i], 0)
}
if nums2[i] > prevA && nums1[i] > prevB {
res = min(res, dfs(i+1, nums2[i], nums1[i], 1) + 1)
}
dp[i][swap] = res
return res
}
return dfs(0, -1, -1, 0)
}
func minSwap1(nums1 []int, nums2 []int) int {
n := len(nums1)
dp := make([][2]int, n)
dp[0][0], dp[0][1] = 0, 1
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 1; i < n; i++ {
dp[i] = [2]int{n, n} // 答案不会超过 n,故初始化成 n 方便后面取 min
if nums1[i-1] < nums1[i] && nums2[i-1] < nums2[i] {
dp[i][0] = dp[i-1][0]
dp[i][1] = dp[i-1][1] + 1
}
if nums2[i-1] < nums1[i] && nums1[i-1] < nums2[i] {
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])
}
func main() {
// Example 1:
// Input: nums1 = [1,3,5,4], nums2 = [1,2,3,7]
// Output: 1
// Explanation:
// Swap nums1[3] and nums2[3]. Then the sequences are:
// nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
// which are both strictly increasing.
fmt.Println(minSwap([]int{1,3,5,4}, []int{1,2,3,7})) // 1
// Example 2:
// Input: nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
// Output: 1
fmt.Println(minSwap([]int{0,3,5,8,9}, []int{2,1,4,6,9})) // 1
fmt.Println(minSwap1([]int{1,3,5,4}, []int{1,2,3,7})) // 1
fmt.Println(minSwap1([]int{0,3,5,8,9}, []int{2,1,4,6,9})) // 1
}