C++ beat 99.3% with clear explanations
\nclass Solution {\npublic:\n    int minSwap(vector<int> &A, vector<int> &B) {\n        int vec_len = A.size();\n        /*dp[i][0]: the cost if do not exchange
```\nclass Solution {\npublic:\n    int minSwap(vector<int> &A, vector<int> &B) {\n        int vec_len = A.size();\n        /*dp[i][0]: the cost if do not exchange at position i. dp[i][1]: the cost of changing at position i*/\n        vector <vector<int>> dp = vector <vector< int >> (vec_len, vector<int>(2, INT32_MAX));\n        dp[0][0] = 0;\n        dp[0][1] = 1;\n        for (int i = 1; i < vec_len; ++i) {\n            if (A[i] > A[i - 1] && B[i] > B[i - 1]) {\n                /*condition already satisfied, no change. if last position has changed,\n                 the current one needs to change to keep ascending order*/\n                dp[i][0] = dp[i - 1][0];\n                dp[i][1] = dp[i - 1][1] + 1;\n            }\n            if (A[i] > B[i - 1] && B[i] > A[i - 1]) {\n                /*if change is not harmful, you can either change the last position or not */\n                dp[i][0] = min(dp[i][0], dp[i - 1][1]);\n                dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1);\n            }\n        }\n\n        return min(dp[vec_len - 1][0], dp[vec_len - 1][1]);\n    }\n};\n```
[]