Hope It helps dp O(n)
inspired from @SolaAoi  \n\nclass Solution {\npublic:\n    int minSwap(vector<int>& A, vector<int>& B) \n    {\n        int n=A.size();\n        \n        int d
inspired from @SolaAoi  \n```\nclass Solution {\npublic:\n    int minSwap(vector<int>& A, vector<int>& B) \n    {\n        int n=A.size();\n        \n        int dp[n][2];\n        \n        \n        for(int i=0;i<n;i++)\n        {\n            dp[i][0]=INT_MAX;\n            dp[i][1]=INT_MAX;\n        }\n        \n        \n        dp[0][0]=0;\n        dp[0][1]=1;\n        \n        \n         for (int i = 1; i < n; ++i) \n         {\n            if (A[i] > A[i - 1] && B[i] > B[i - 1]) \n            {\n                \n//                 we donot swap the A[i],B[i]  the answer is same as prev one\n                dp[i][0] = dp[i - 1][0];\n//                 if we swap the to make it in order we have to use the prev swapped result\n                \n//                 3 6 -------->  3 9  to make it order  we have to swap previous pair 8 9 \n//                 8 9            8 6                                                  3 6 \n                \n                dp[i][1] = dp[i - 1][1] + 1;\n            }\n            if (A[i] > B[i - 1] && B[i] > A[i - 1])\n            {\n                \n//                 3 15   \n//                 9 12\n//              we have two choices \n//                  1)not the current pair\n//                  2)swap\n                \n//                 1)  in  choice we have another option that is we can swap prev pair  but then order is maintaines\n                \n//                 3 15 ----> 9 15  we can see the order is maintained\n//                 9 12       3 12\n                dp[i][0] = min(dp[i][0], dp[i - 1][1]);\n                \n//                   if we swap the current pair the also then order is maintained there is no need to swap the prev pair \n                \n//                 3 15 -----> 3 12\n//                 9 12        9 15\n                dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1);\n            }\n        }\n        \n         return min(dp[n-1][0],dp[n-1][1]);\n    }\n        \n        \n};\n```
[]