C++ Solution| Dp | O(n) | Well commented code and well explained
Here we are taking dp[n][2] where for any index i there are 2 case-\n-->dp[i][0] - when we don\'t want to swap the ith state\n-->dp[i][1]- when we want to swap 
Here we are taking dp[n][2] where for any index i there are 2 case-\n-->dp[i][0] - when we don\'t want to swap the ith state\n-->dp[i][1]- when we want to swap the ith state \nThen for any index i we have two situations as follows-\n--> A[i-1]<A[i] && B[i-1]<B[i]   then\n\t\t------> if we don\'t swap the ith state dp[i][0]=dp[i-1][0]\n\t\t------>if we swap the ith state then we need to swap the i-1th state also dp[i][1]=dp[i-1][1]+1  //+1 is cost of swapping\n-->A[i-1]<B[i] && B[i-1]<A[i] then\n------->if we don\'t swap ith index we need to swap the i-1th state dp[i][0]=dp[i-1][1]\n------->if we swap the ith index then we don\'t need to swap the i-1th state dp[i][1]=dp[i-1][0]+1    //+1 is cost of swaping\n```\nclass Solution {\npublic:\n    int minSwap(vector<int>& A, vector<int>& B) {\n        int n=A.size();\n        vector<vector<int>> dp(n,vector<int>(2,INT_MAX));\n        //base case\n        dp[0][0]=0;//if we don\'t swap no cost\n        dp[0][1]=1;// if we swap then cost is 1\n        for(int i=1;i<n;i++)\n        {\n            if(A[i-1]<A[i] && B[i-1]<B[i])//this is the when the array is increasing\n            {\n                dp[i][0]=min(dp[i][0],dp[i-1][0]);//if ith is unchanged it would have value of dp[i-1][0] since the i-1th state will also remain unchanged\n                dp[i][1]=min(dp[i][1],dp[i-1][1]+1);//if ith state is change that means that i-1th state must also be changed because the are following the rule of increasing\n            }\n            if(A[i-1]<B[i] && B[i-1]<A[i])//this is the case when array is not increasing\n            {\n                dp[i][0]=min(dp[i][0],dp[i-1][1]);//if we donot swap the ith state it means the we are swapping 0..(i-1) th state and ith state remains unchanged\n                dp[i][1]=min(dp[i][1],dp[i-1][0]+1);//if we are swaping the ith state then it means that we are not swaping the 0....(i-1)th state\n            }\n        }\n        int ans=min(dp[n-1][0],dp[n-1][1]);\n        return ans;\n    }\n};
[]