-
Notifications
You must be signed in to change notification settings - Fork 43
/
minimum-swaps-to-make-sequences-increasing.py
105 lines (100 loc) · 3.58 KB
/
minimum-swaps-to-make-sequences-increasing.py
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
97
98
99
100
101
102
103
104
105
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/83269027
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])
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/83269027
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])
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/83269027
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)
# V1'''
# https://www.jiuzhang.com/solution/minimum-swaps-to-make-sequences-increasing/#tag-highlight-lang-python
class Solution:
def minSwap(self, A, B):
if len(A) == 0 or len(A) != len(B):
return 0
non_swapped, swapped = [0] * len(A), [1] + [0] * (len(A) - 1)
for i in range(1, len(A)):
swps, no_swps = set(), set()
if A[i - 1] < A[i] and B[i - 1] < B[i]:
swps.add(swapped[i - 1] + 1)
no_swps.add(non_swapped[i - 1])
if B[i - 1] < A[i] and A[i - 1] < B[i]:
swps.add(non_swapped[i - 1] + 1)
no_swps.add(swapped[i - 1])
swapped[i], non_swapped[i] = min(swps), min(no_swps)
return min(swapped[-1], non_swapped[-1])
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def minSwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
dp_no_swap, dp_swap = [0]*2, [1]*2
for i in range(1, len(A)):
dp_no_swap[i%2], dp_swap[i%2] = float("inf"), float("inf")
if A[i-1] < A[i] and B[i-1] < B[i]:
dp_no_swap[i%2] = min(dp_no_swap[i%2], dp_no_swap[(i-1)%2])
dp_swap[i%2] = min(dp_swap[i%2], dp_swap[(i-1)%2]+1)
if A[i-1] < B[i] and B[i-1] < A[i]:
dp_no_swap[i%2] = min(dp_no_swap[i%2], dp_swap[(i-1)%2])
dp_swap[i%2] = min(dp_swap[i%2], dp_no_swap[(i-1)%2]+1)
return min(dp_no_swap[(len(A)-1)%2], dp_swap[(len(A)-1)%2])