[Python] DP solution with explanations
Explanations: For each i, we can either swap or keep the two respective value. Since the best options can come from either swap or keep as well, we need to have
**Explanations**: For each `i`, we can either swap or keep the two respective value. Since the best options can come from either swap or keep as well, we need to have 2 array to record two actions at each `i`. Here, `dp[0][:]` will record keep and `dp[1][:]` will record swap. \nFor `dp[0][i]`, number of swaps needed, if at `i`, we do nothing. (Therefore, `dp[0][0] = 0`)\nFor `dp[1][i]`, number of swaps needed, if at `i`, we swap `A[i]` and `B[i]`. (Therefore, `dp[1][0] = 1`)\n\nThere are two cases that will result in keep and swap:\n1.  `A[i] > A[i - 1] and B[i] > B[i - 1]`: A, B are both increasing. \n     If at `i`, we choose to keep, `i-1` will also have to keep to make it valid.\n\t If at `i`, we choose to swap, `i-1` will also have to swap to make it valid and since we swap at `i`, we need to add one.\n2.  `A[i] > B[i - 1] and B[i] > A[i - 1]`: A is bigger than B\' prior and B is bigger than A\'s prior.\n     If at `i`, we choose to keep, `i-1` will have to swap to make it valid. We also have to compare and restore the smallest number of swaps for `dp[0][i]` incase Case 1 is also valid statement.\n\t If at `i`, we choose to swap, `i-1` will have to keep.\n\n**Complexity**: Time O(N), N is length of A. Space O(N)\n```\nclass Solution:\n    def minSwap(self, A: List[int], B: List[int]) -> int:\n        n = len(A)\n        dp = [[float(\'inf\')] * n for _ in range(2)]\n        dp[0][0], dp[1][0] = 0, 1 # keep # swap \n        for i in range(1, n):\n            if A[i] > A[i - 1] and B[i] > B[i - 1]:\n                # keep i\n                dp[0][i] = dp[0][i - 1]\n                # swap i\n                dp[1][i] = dp[1][i - 1] + 1\n            if A[i] > B[i - 1] and B[i] > A[i - 1]:\n                # keep i\n                dp[0][i] = min(dp[0][i], dp[1][i - 1])\n                # swap i\n                dp[1][i] = min(dp[1][i], dp[0][i - 1] + 1)\n        return min(dp[0][n - 1], dp[1][n - 1])\n```
['Dynamic Programming']