Recursive wit memo in C++
\nclass Solution {\npublic:\n    \n    int knap(vector<int>&A, vector<int>&B, int prevA, int prevB, int swap, int idx, vector<vector<int>> &dp) {\n        \n   
```\nclass Solution {\npublic:\n    \n    int knap(vector<int>&A, vector<int>&B, int prevA, int prevB, int swap, int idx, vector<vector<int>> &dp) {\n        \n        if(idx==A.size()){\n            return 0;\n        }\n        \n        if(dp[idx][swap]!=-1){\n            return dp[idx][swap];\n        }\n        int minswaps = INT_MAX;\n        \n        if(A[idx]>prevA && B[idx]>prevB){\n            minswaps = knap(A,B,A[idx],B[idx],0,idx+1,dp);\n        }\n        \n        if(B[idx]>prevA && A[idx]>prevB){\n        minswaps = min(minswaps, knap(A,B,B[idx],A[idx],1,idx+1,dp)+1);\n        }\n        \n        return dp[idx][swap] = minswaps;\n    }\n    \n    \n    \n    \n    \n    \n    int minSwap(vector<int>& A, vector<int>& B) {\n        //int dp[A.size()+1][2];\n        vector<vector<int>>dp(A.size()+1, vector<int>(2,-1));\n        //emset(dp,-1,sizeof(dp));\n        return knap(A,B,-1,-1,0,0,dp);\n    }\n};\n```
[]