C++ O(n) time, O(1) space
cpp []\nclass Solution {\npublic:\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        int swap = 1;\n        int noSwap = 0;\n        \n        
```cpp []\nclass Solution {\npublic:\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        int swap = 1;\n        int noSwap = 0;\n        \n        for (int i = 1; i < nums1.size(); i++) {\n            int curSwap = 0;\n            int curNoSwap = 0;\n\n            if (nums1[i] <= nums1[i-1] || nums2[i] <= nums2[i-1]) {\n                // musn\'t have swapped if swapping; else must have swapped\n                curSwap = noSwap + 1;\n                curNoSwap = swap;\n            } else if (nums1[i] <= nums2[i-1] || nums2[i] <= nums1[i-1]) {\n                // must have swapped if swapping; else musn\'t have\n                curSwap = swap + 1;\n                curNoSwap = noSwap;\n            } else {\n                curSwap = min(swap, noSwap) + 1;\n                curNoSwap = min(swap, noSwap);\n            }\n\n            swap = curSwap;\n            noSwap = curNoSwap;\n        }\n\n        return min(swap, noSwap);\n    }\n};\n```
['C++']