C++ dp solution with simple explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nTo find min number of swaps at a particular position, following information at previous
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo find min number of swaps at a particular position, following information at previous position is required \n-min swaps if swap at previous position\n-min swaps if no swap at previous position\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor each position, find minimum number of swaps if current position is swapped, also if current position is not swapped.\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$ (can be easily optimized, since only information about previous position is required)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minSwap(vector<int>& v1, vector<int>& v2) {\n        int n=v1.size();\n        int dp[n][2];\n        for(int i=0;i<n;i++){dp[i][0]=INT_MAX; dp[i][1]=INT_MAX;}\n        dp[0][0]=0;\n        dp[0][1]=1;\n        int prev1,prev2;\n        for(int i=1;i<n;i++){\n            //prev position swapped\n            prev1=v2[i-1];\n            prev2=v1[i-1];\n            if(dp[i-1][1]!=INT_MAX){\n                //swap at current position\n                if(v2[i]>prev1 && v1[i]>prev2){\n                    dp[i][1]=1+dp[i-1][1];\n                }\n                //no swap at current position\n                if(v1[i]>prev1 && v2[i]>prev2){\n                    dp[i][0]=dp[i-1][1];\n                }\n            }\n\n            //prev position no swap\n            prev1=v1[i-1];\n            prev2=v2[i-1];\n            if(dp[i-1][0]!=INT_MAX){\n                //swap at current position\n                if(v2[i]>prev1 && v1[i]>prev2){\n                    dp[i][1]=min(dp[i][1],1+dp[i-1][0]);\n                }\n                //no swap at current position\n                if(v1[i]>prev1 && v2[i]>prev2){\n                    dp[i][0]=min(dp[i][0],dp[i-1][0]);\n                }\n            }\n        }\n        return min(dp[n-1][0],dp[n-1][1]);\n    }\n};\n```
['C++']