Simple Python Top-Down DFS + @cache (12 lines)
dfs(i, a, b) returns the minimum swaps to ensure that a < nums1[i] and b < nums2[i]. If nums1[i] and nums2[i] are already greater than the things that come befo
`dfs(i, a, b)` returns the minimum swaps to ensure that `a < nums1[i]` and `b < nums2[i]`. If `nums1[i]` and `nums2[i]` are already greater than the things that come before we don\'t need to swap, otherwise we have to swap.\n\nIt\'s still O(n)\n\n```\nfrom functools import cache\nclass Solution:\n    def minSwap(self, nums1: List[int], nums2: List[int]) -> int:\n        @cache\n        def dfs(i, a, b):\n            if i == len(nums1): return 0\n            min_swaps = float(\'inf\')\n            if a < nums1[i] and b < nums2[i]:\n                min_swaps = min(min_swaps, dfs(i+1, nums1[i], nums2[i]))\n            if a < nums2[i] and b < nums1[i]:\n                min_swaps = min(min_swaps, 1+dfs(i+1, nums2[i], nums1[i]))\n            return min_swaps\n        return dfs(0, float(\'-inf\'), float(\'-inf\'))\n```
['Depth-First Search', 'Python', 'Python3']