Solution
C++ []\nconst int ZERO = []() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    return 0;\n}();\nclass Solution {\npublic:\n    int minSwap(v
```C++ []\nconst int ZERO = []() {\n    ios_base::sync_with_stdio(false);\n    cin.tie(nullptr);\n    return 0;\n}();\nclass Solution {\npublic:\n    int minSwap(vector<int>& a, vector<int>& b) {\n        int n = static_cast<int>(a.size());\n        vector<int> swaps(n, n);\n        vector<int> noswaps(n, n);\n        swaps[0] = 1;\n        noswaps[0] = 0;\n        for (int i = 1; i < n; ++i) {\n            if (a[i] > a[i-1] && b[i] > b[i-1]) {\n                swaps[i] = swaps[i-1] + 1;\n                noswaps[i] = noswaps[i-1];\n            }\n            if (a[i] > b[i-1] && b[i] > a[i-1]) {\n                swaps[i] = min(swaps[i], noswaps[i-1] + 1);\n                noswaps[i] = min(noswaps[i], swaps[i-1]);\n            }\n        }\n        return min(swaps.back(), noswaps.back());\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def minSwap(self, A: List[int], B: List[int]) -> int:\n        ans = sm = lg = mx = 0\n        for x, y in zip(A, B): \n            if mx < min(x, y):\n                ans += min(sm, lg)\n                sm = lg = 0 \n            mx = max(x, y)\n            if x < y: sm += 1 # count "x < y"\n            elif x > y: lg += 1 # count "x > y"\n        return ans + min(sm, lg)\n```\n\n```Java []\nclass Solution {\n    public int minSwap(int[] A, int[] B) {\n        int swapRecord = 1, fixRecord = 0;\n        for (int i = 1; i < A.length; i++) {\n            if (A[i - 1] >= B[i] || B[i - 1] >= A[i]) {\n                swapRecord++;\n            } else if (A[i - 1] >= A[i] || B[i - 1] >= B[i]) {\n                int temp = swapRecord;\n                swapRecord = fixRecord + 1;\n                fixRecord = temp;\n            } else {\n                int min = Math.min(swapRecord, fixRecord);\n                swapRecord = min + 1;\n                fixRecord = min;\n            }\n        }\n        return Math.min(swapRecord, fixRecord);\n    }\n}\n```\n
['C++', 'Java', 'Python3']