DP Simple O(n) time O(1) Space with detailed explanation
Think of the problem as finding the best path from n-1th index to nth index\nSuppose Array \n        A=[0,7,8,10,10,11,12,13,19,18] and\n\t    B=[4,4,5,  7,11,1
Think of the problem as finding the best path from n-1th index to nth index\nSuppose Array \n        A=[0,7,8,10,10,11,12,13,19,18] and\n\t    B=[4,4,5,  7,11,14,15,16,17,20]\n\t\t\t\t\t\t\t\t\t\t\t         ^\nInitially,\t\t\t\t\t\t\t\t\t\t\t \nswap    =[0,0,0,0,0,0,0,0,0,1]\nunswap=[0,0,0,0,0,0,0,0,0,0]\n\t\t\t\t\t\t\t\t\t\t   ^\t\t\t\t\t\t\t\t\t\t   \nImplies If we are at last index we can swap the values(cost=1) and reach destination OR we can directly reach without swap(cost=0)\n\nWe can encounter 3 cases while traversing the array from right to left:\n```1 case:``` Both A[i] and B[i] are in strictly increasing order\n```A[i]<A[i+1] && A[i]<B[i+1] &&B[i]<A[i+1]&& B[i]<B[i+1]```\nHere the current action will be independent of next values \ni.e.```    swap[i]=1+ min(swap[i+1],unswap[i+1])```\n```     unswap[i]=0+ min(swap[i+1],unswap[i+1])```\n\n```2 case:```Either A or B violates its own sequence.\n```A[i]>=A[i+1] || B[i]>=B[i+1]```\nHere we have to make sure that current and next are alternate.\nImplies either swap current and unswap next OR swap next and unswap current\ni.e.```    swap[i]=1+ unswap[i+1]```\n```     unswap[i]=0+ swap[i+1]```\n\n```3 case:```Either A or B violates  the other\'s sequence.\n```A[i]>=B[i+1] || B[i]>=A[i+1]```\nHere we have to make sure that current and next are in same order.\nImplies either swap current and swap next OR unswap current and unswap next\ni.e.```    swap[i]=1+ swap[i+1]```\n```     unswap[i]=0+ unswap[i+1]```\n\nThe code in JavaScript is as follows\n```\n/**\n * @param {number[]} A\n * @param {number[]} B\n * @return {number}\n */\nvar minSwap = function(A, B) {\n    let swap = new Array(A.length).fill(0);\n    let unswap = new Array(A.length).fill(0);\n    swap[A.length-1]=1;\n    unswap[A.length-1]=0;\n    for(let i=A.length-2;i>=0;i--){\n        /*Case 1 a<c,d &b <c,d*/\n        if(A[i]<A[i+1]&&A[i]<B[i+1]&&B[i]<A[i+1]&&B[i]<B[i+1]){\n            swap[i]=1+Math.min(swap[i+1],unswap[i+1]);\n            unswap[i]=0+Math.min(swap[i+1],unswap[i+1]);\n            continue;\n        }\n        /*Case 2 a>=c || b>=d*/\n        if(A[i]>=A[i+1]||B[i]>=B[i+1]){\n            swap[i]=1+unswap[i+1];\n            unswap[i]=0+swap[i+1];\n            continue;\n        }\n        /*Case 3 a>=d ||b >=c*/    \n        if(A[i]>=B[i+1]||B[i]>=A[i+1]){\n            swap[i]=1+swap[i+1];\n            unswap[i]=0+unswap[i+1];\n            continue;\n        }\n        \n    }\n    return Math.min(swap[0],unswap[0]);\n};\n```\n\nWe only need 2 variables so we can optimize space  and make it O(1) Space\n```\n/**\n * @param {number[]} A\n * @param {number[]} B\n * @return {number}\n */\nvar minSwap = function(A, B) {\n    let swap=1;\n    let unswap=0;\n    for(let i=A.length-2;i>=0;i--){\n        let temp=swap;\n        /*Case 1 a<c,d &b <c,d*/\n        if(A[i]<A[i+1]&&A[i]<B[i+1]&&B[i]<A[i+1]&&B[i]<B[i+1]){            \n            swap=1+Math.min(swap,unswap);\n            unswap=0+Math.min(temp,unswap);\n            continue;\n        }\n        /*Case 2 a>=c || b>=d*/\n        if(A[i]>=A[i+1]||B[i]>=B[i+1]){\n            swap=1+unswap;\n            unswap=0+temp;\n            continue;\n        }\n        /*Case 3 a>=d ||b >=c*/    \n        if(A[i]>=B[i+1]||B[i]>=A[i+1]){\n            swap=1+swap;\n            unswap=0+unswap;\n            continue;\n        }\n        \n    }\n    return Math.min(swap,unswap);\n};\n```
[]