class Solution {
int f[1000+10][2];
public:
int minSwap(vector<int>& A, vector<int>& B) {
memset(f,127,sizeof(f));
f[0][0] = 0;
f[0][1] = 1;
int n = A.size();
for(int i = 1;i < n; ++i)
{
if(A[i] > A[i-1] && B[i] > B[i-1]) f[i][0] = min(f[i][0],f[i-1][0]);
if(A[i] > A[i-1] && B[i] > B[i-1]) f[i][1] = min(f[i][1],f[i-1][1]+1);
if(A[i] > B[i-1] && B[i] > A[i-1]) f[i][0] = min(f[i][0],f[i-1][1]);
if(A[i] > B[i-1] && B[i] > A[i-1]) f[i][1] = min(f[i][1],f[i-1][0]+1);
}
return min(f[n-1][0],f[n-1][1]);
}
};