<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>

class Solution {
public:

    vector<int>nums1,nums2;
    int dp[100100][4];

    int f(int ind, int flag){

        // base case : 
        int n = nums1.size();
        if(ind == n){
            return 0;
        }

        // cache : 
        if(dp[ind][flag+1] != -1){
            return dp[ind][flag+1];
        }

        // transition : 
        int ans = 1e9;
        if(flag == -1){ // first elem to be taken : 
            // op1 : stay like this : 
            int op1 = 0 + f(ind + 1, 0);
            // op2 : swap and change flag : 
            int op2 = 1 + f(ind + 1, 1);
            ans = min(op1,op2);
        }else if(flag == 0){ // means both have taken from respective arrays : 
            // check if incr or not : 
            if(nums1[ind] > nums1[ind-1] and nums2[ind] > nums2[ind-1]){
                // this means we can just skip :
                int op1 = 0 + f(ind + 1, 0); 
                // or we can swap if condition holds still : 
                int op2 = 1e9;
                if(nums1[ind] > nums2[ind-1] and nums2[ind] > nums1[ind-1]){
                    op2 = 1 + f(ind + 1, 1);
                }
                ans = min(op1,op2);
            }else{
                int op1 = 1e9;
                if(nums1[ind] > nums2[ind-1] and nums2[ind] > nums1[ind-1]){
                    op1 = 1 + f(ind + 1, 1);
                }
                ans = op1;
            }
        }else{ // means both have taken from opposite arrays : 
            // check if already incr : 
            if(nums1[ind] > nums2[ind-1] and nums2[ind] > nums1[ind-1]){
                // op1 : skip and go back to ur original array
                int op1 = 0 + f(ind + 1, 0);
                // op2 : again take from opp array : 
                int op2 = 1e9;
                if(nums1[ind] > nums1[ind-1] and nums2[ind] > nums2[ind-1]){
                    op2 = 1 + f(ind + 1, 1);
                }
                ans = min(op1,op2);
            }else{
                int op1 = 1e9;
                if(nums1[ind] > nums1[ind-1] and nums2[ind] > nums2[ind-1]){
                    op1 = 1 + f(ind + 1, 1);
                }
                ans = op1;
            }
        }

        // store and return ans : 
        return dp[ind][flag+1] = ans;
    }

    int minSwap(vector<int>& n1, vector<int>& n2) {

        // logic : 
        // at each ind we have a choice, whether to swap or not
        // if nums1[i] < nums1[i-1] then we have to swap with nums2 
        // and condition must satisfy w nums2 as well else return 1e9

        // state : 
        // dp[i][flag1][flag2] is the min number of swaps needed to make both seq num[i...n] increasing
        // with prev elem from flag array. if f = 0, from nums1, f = 1, from nums2
        // flag1 indicates from which nums1 took from prev 
        // flag2 is which nums2 took prev
        // obv both will be complementary

        // if flag = 0 means nums1 took from nums1 itself
        // flag = 1 means nums1 took from nums2
        // similarly for nums2, flag = 0 means it took from nums2 else nums1

        // transition : 
        // if(nums1[i] > nums1[i-1]) then no compulsion to swap : 0 + f(ind + 1,flag)
        // if we swap : if(nums2[i] > nums1[i-1] and nums1[i] > nums2[i-1]) : 1 + f(ind + 1,!flag)
        // if(nums1[i] <= nums1[i-1]) then we have to swap
        // the only thing is, we need to check both arrays simultaneously

        // base case : 
        // ind == n return 0

        // final subproblem : 
        // return f(0,0)

        nums1 = n1;
        nums2 = n2;
        memset(dp,-1,sizeof(dp));
        return f(0,-1);
    }
};
108271