Python DP solution O(n) time O(1) space
\nclass Solution:\n    def minSwap(self, A: List[int], B: List[int]) -> int:\n        \n        # DP\n        # \n        # state: f(i, s) := min swaps to make 
```\nclass Solution:\n    def minSwap(self, A: List[int], B: List[int]) -> int:\n        \n        # DP\n        # \n        # state: f(i, s) := min swaps to make A[:i+1] B[:i+1] strckly increasing, s indicating if last bit is swapped or not\n        # funct: \n        #        f(i, 0) = min(f(i-1, 0), f(i-1, 1)) (check if strictly increasing with the last 2 steps)\n        #        f(i, 1) = min(f(i-1, 0), f(i-1, 1)) + 1 (check if strictly increasing with the last 2 steps)\n        # start: f(0, 0) = 0, f(0, 1) = 1\n        # final: min(f(n, 0), f(n, 1))\n        #\n        # Can trivially modify code and optimize to constant space since each dp cell only depends on last cell\n        # Kept it this way to make it a bit more readable.\n        \n        dp = [[None] * 2 for _ in range(len(A))]\n        dp[0][0], dp[0][1] = 0, 1\n        for i in range(1, len(A)):\n            dp[i][0] = min([dp[i-1][0] if A[i] > A[i-1] and B[i] > B[i-1] else float(\'inf\'),\n                            dp[i-1][1] if A[i] > B[i-1] and B[i] > A[i-1] else float(\'inf\')])\n            dp[i][1] = min([dp[i-1][0] if B[i] > A[i-1] and A[i] > B[i-1] else float(\'inf\'),\n                            dp[i-1][1] if B[i] > B[i-1] and A[i] > A[i-1] else float(\'inf\')]) + 1\n        return min(dp[-1][0], dp[-1][1])\n```\n\nUpdate: The O(1) space version\n```\n        dp = [0, 1]\n        for i in range(1, len(A)):\n            dp = [min([dp[0] if A[i] > A[i-1] and B[i] > B[i-1] else float(\'inf\'),\n                       dp[1] if A[i] > B[i-1] and B[i] > A[i-1] else float(\'inf\')]),\n                  min([dp[0] if B[i] > A[i-1] and A[i] > B[i-1] else float(\'inf\'),\n                       dp[1] if B[i] > B[i-1] and A[i] > A[i-1] else float(\'inf\')]) + 1]\n        return min(dp)\n```
[]