<p>You are given two integer arrays of the same length <code>nums1</code> and <code>nums2</code>. In one operation, you are allowed to swap <code>nums1[i]</code> with <code>nums2[i]</code>.</p>

<ul>
	<li>For example, if <code>nums1 = [1,2,3,<u>8</u>]</code>, and <code>nums2 = [5,6,7,<u>4</u>]</code>, you can swap the element at <code>i = 3</code> to obtain <code>nums1 = [1,2,3,4]</code> and <code>nums2 = [5,6,7,8]</code>.</li>
</ul>

<p>Return <em>the minimum number of needed operations to make </em><code>nums1</code><em> and </em><code>nums2</code><em> <strong>strictly increasing</strong></em>. The test cases are generated so that the given input always makes it possible.</p>

<p>An array <code>arr</code> is <strong>strictly increasing</strong> if and only if <code>arr[0] &lt; arr[1] &lt; arr[2] &lt; ... &lt; arr[arr.length - 1]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,3,5,4], nums2 = [1,2,3,7]
<strong>Output:</strong> 1
<strong>Explanation:</strong> 
Swap nums1[3] and nums2[3]. Then the sequences are:
nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
which are both strictly increasing.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums1.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums2.length == nums1.length</code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 2 * 10<sup>5</sup></code></li>
</ul>

def f1(nums1, nums2, idx, p1, p2):
    if idx == len(nums1):
        return 0

    ans = float("inf")

    if nums1[idx] > p1 and nums2[idx] > p2: # no swap
        ans = min(ans, 0 + f1(nums1, nums2, idx+1, nums1[idx], nums2[idx]))
    
    if nums1[idx] > p2 and nums2[idx] > p1: # swap
        ans = min(ans, 1 + f1(nums1, nums2, idx+1, nums2[idx], nums1[idx]))

    return ans

def f1_2(nums1, nums2, idx, prev_swapped):
    if idx == len(nums1):
        return 0

    ans = float("inf")
    if prev_swapped == 1:
        p1 = nums2[idx-1]
        p2 = nums1[idx-1]
    else:
        p2 = nums2[idx-1]
        p1 = nums1[idx-1]

    if nums1[idx] > p1 and nums2[idx] > p2: # no swap
        ans = min(ans, 0 + f1_2(nums1, nums2, idx+1, 0))
    
    if nums1[idx] > p2 and nums2[idx] > p1: # swap
        ans = min(ans, 1 + f1_2(nums1, nums2, idx+1, 1))

    return ans

def f2(nums1, nums2, idx, p1, p2, DP):
    if idx == len(nums1):
        return 0
    key = (idx, p1, p2)
    if key in DP:
        return DP[key]

    ans = float("inf")

    if nums1[idx] > p1 and nums2[idx] > p2: # no swap
        ans = min(ans, 0 + f2(nums1, nums2, idx+1, nums1[idx], nums2[idx], DP))
    
    if nums1[idx] > p2 and nums2[idx] > p1: # swap
        ans = min(ans, 1 + f2(nums1, nums2, idx+1, nums2[idx], nums1[idx], DP))

    DP[key] = ans
    return ans

def f2_2(nums1, nums2, idx, prev_swapped, DP):
    if idx == len(nums1):
        return 0

    if DP[idx][prev_swapped] != -1:
        return DP[idx][prev_swapped]

    ans = float("inf")
    if prev_swapped == 1:
        p1 = nums2[idx-1]
        p2 = nums1[idx-1]
    else:
        p2 = nums2[idx-1]
        p1 = nums1[idx-1]

    if nums1[idx] > p1 and nums2[idx] > p2: # no swap
        ans = min(ans, 0 + f2_2(nums1, nums2, idx+1, 0, DP))
    
    if nums1[idx] > p2 and nums2[idx] > p1: # swap
        ans = min(ans, 1 + f2_2(nums1, nums2, idx+1, 1, DP))

    DP[idx][prev_swapped] = ans
    return ans

def f3(nums1, nums2, idx, p1, p2, DP):
    if idx == len(nums1):
        return 0
    key = (idx, p1, p2)
    if key in DP:
        return DP[key]

    ans = float("inf")

    if nums1[idx] > p1 and nums2[idx] > p2: # no swap
        ans = min(ans, 0 + f2(nums1, nums2, idx+1, nums1[idx], nums2[idx], DP))
    
    if nums1[idx] > p2 and nums2[idx] > p1: # swap
        ans = min(ans, 1 + f2(nums1, nums2, idx+1, nums2[idx], nums1[idx], DP))

    DP[key] = ans
    return ans

class Solution:
    def minSwap(self, nums1: List[int], nums2: List[int]) -> int:
        """
        we have to find minimum of possible cases

        case1: already both sorted, no swap

        case2: swapping is possible, swap
        """
        nums1.insert(0, -1)
        nums2.insert(0, -1)
        
        # return f1(nums1, nums2, 0, -1, -1)

        # return f1_2(nums1, nums2, 1, prev_swapped=0)

        # DP = {}
        # return f2(nums1, nums2, 0, -1, -1, DP)

        DP = [[-1 for _ in range(2)] for _ in range(len(nums1))]
        return f2_2(nums1, nums2, 1, prev_swapped=0, DP=DP)

        # return f3(nums1, nums2)
62529