Python DP solution with explanation
\nclass Solution(object):\n    def minSwap(self, A, B):\n        if len(A) != len(B) or len(A) == 0:\n            return -1        \n                \n        #
```\nclass Solution(object):\n    def minSwap(self, A, B):\n        if len(A) != len(B) or len(A) == 0:\n            return -1        \n                \n        #it shows the starting status at 0 position\n        #pkeep stores the previous no swap minimum value, so at 0 position, the value is 0 if no swap\n        #pswap stores the previous swap minimum value, so at 0 postion, the value is 1 if swap\n        pkeep, pswap = 0, 1 \n        \n        \n        for i in range(1, len(A)):\n            keep = swap = float(\'inf\') #they are the current statuses\' values, by default we put maximum as initialization\n            \n            #both A and B are in ascend order for i and i-1 elements\n            #we can choose to swap both or not at all\n            #pay attention here, we can\'t just swap i or i-1, because the if condition doesn\'t guarantee A[i] > B[i]\n            if A[i] > A[i-1] and B[i] > B[i-1]: \n                keep = pkeep \n                swap = pswap+1 \n                \n            #the if condition checks A and B are in ascending trend for crossing case\n            #we can choose to swap i or swap i-1, but you can\'t swap twice\n            if A[i] > B[i-1] and B[i] > A[i-1]:\n                keep = min(keep, pswap)\n                swap = min(pkeep+1, swap)\n            #finally we make sure the status is propagated to the next \n            pswap = swap\n            pkeep = keep\n        \n        return min(keep, swap)\n```
[]