-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path801.cpp
More file actions
22 lines (22 loc) · 689 Bytes
/
Copy path801.cpp
File metadata and controls
22 lines (22 loc) · 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int minSwap(vector<int> &A, vector<int> &B) {
int n = A.size();
if (n == 0)
return 0;
vector<vector<int>> dp(2, vector<int>(n, INT_MAX));
dp[0][0] = 0;
dp[1][0] = 1;
for (int i = 1; i < n; ++i) {
if (A[i - 1] < A[i] && B[i - 1] < B[i]) {
dp[0][i] = dp[0][i - 1];
dp[1][i] = dp[1][i - 1] + 1;
}
if (A[i - 1] < B[i] && B[i - 1] < A[i]) {
dp[0][i] = min(dp[0][i], dp[1][i - 1]);
dp[1][i] = min(dp[1][i], dp[0][i - 1] + 1);
}
}
return min(dp[0][n - 1], dp[1][n - 1]);
}
};