Python recursion with cache
\n   def _minSwap(self, A, B, i, cache, swapped):\n        if (swapped, i) in cache:\n            return cache[(swapped, i)]\n        if i == len(A):\n         
```\n   def _minSwap(self, A, B, i, cache, swapped):\n        if (swapped, i) in cache:\n            return cache[(swapped, i)]\n        if i == len(A):\n            return 0\n        l_min = math.inf if i > 0 and (A[i] <= A[i - 1] or B[i] <= B[i - 1]) else self._minSwap(A, B, i + 1, cache, 0)\n        A[i], B[i] = B[i], A[i]\n        r_min = math.inf if i > 0 and (A[i] <= A[i - 1] or B[i] <= B[i - 1]) else self._minSwap(A, B, i + 1, cache, 1)\n        A[i], B[i] = B[i], A[i]\n        cache[(swapped, i)] = min(l_min, r_min + 1)\n        return cache[(swapped, i)]\n\n    def minSwap(self, A, B):\n        if len(A) == 1: return 0\n        return self._minSwap(A, B, 0, {}, 0)\n```
[]