Sliding window||45ms Beats 99.61%
Intuition\n Describe your first thoughts on how to solve this problem. \nUse sliding window. Since the array is circular, the right pointer r should go to til n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse sliding window. Since the array is circular, the right pointer r should go to til `n+n0` or `n+n1` to find the circular subinterval of length `n0` (`n1`) all 1s (0s) with minimal swaps.\n\nLater try optimization. 2nd C++ tries to reduce the number of if-branches & use `n0=count(nums, 0)`.\n# Approach\n[Please turn English subtitles if necessary]\n[https://youtu.be/KZHcVDVmJLY?si=5RVFUxbQ4HW1TfBl](https://youtu.be/KZHcVDVmJLY?si=5RVFUxbQ4HW1TfBl)\n<!-- Describe your approach to solving the problem. -->\n1. Use count to count how many 1 are in `nums` & store in `n1`. Set `mswap=INT_MAX`, `cnt1=0`.\n2. Use loop from l=0, r=0 while `r<n+n1` with step r++ do the following\n```\ncnt1+= nums[r%n];\nif (r-l+1 > n1) \n    cnt1-=nums[l++ % n];\n            \nif (r-l+1 == n1) // change all into 1 for nums[l+1..r]\n    mswap = min(mswap, n1-cnt1);\n```\n3. `mswap` is the answer\n4. 2nd approach tries to reduce the number if-branches.\n5. The initial 2 pointers are set l=0, r=n0. cnt1 is initialized with `count(nums[0..n0-1], 1)` so that if-branches are removed.\n# Real RUN for testcase\nConsider the testcase=`[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]`\nAdd some output in the 2nd C++, one has\n\n```\nnums:\n[1010101010101010101010101010101010101010101]\nUse cnt1 times swaps to obtain the following form:\n[0000000000000000000001111111111111111111111]\tcnt1=11 mswap=11\n[1000000000000000000000111111111111111111111]\tcnt1=10 mswap=10\n[1100000000000000000000011111111111111111111]\tcnt1=11 mswap=10\n[1110000000000000000000001111111111111111111]\tcnt1=10 mswap=10\n[1111000000000000000000000111111111111111111]\tcnt1=11 mswap=10\n[1111100000000000000000000011111111111111111]\tcnt1=10 mswap=10\n[1111110000000000000000000001111111111111111]\tcnt1=11 mswap=10\n[1111111000000000000000000000111111111111111]\tcnt1=10 mswap=10\n[1111111100000000000000000000011111111111111]\tcnt1=11 mswap=10\n[1111111110000000000000000000001111111111111]\tcnt1=10 mswap=10\n[1111111111000000000000000000000111111111111]\tcnt1=11 mswap=10\n[1111111111100000000000000000000011111111111]\tcnt1=10 mswap=10\n[1111111111110000000000000000000001111111111]\tcnt1=11 mswap=10\n[1111111111111000000000000000000000111111111]\tcnt1=10 mswap=10\n[1111111111111100000000000000000000011111111]\tcnt1=11 mswap=10\n[1111111111111110000000000000000000001111111]\tcnt1=10 mswap=10\n[1111111111111111000000000000000000000111111]\tcnt1=11 mswap=10\n[1111111111111111100000000000000000000011111]\tcnt1=10 mswap=10\n[1111111111111111110000000000000000000001111]\tcnt1=11 mswap=10\n[1111111111111111111000000000000000000000111]\tcnt1=10 mswap=10\n[1111111111111111111100000000000000000000011]\tcnt1=11 mswap=10\n[1111111111111111111110000000000000000000001]\tcnt1=10 mswap=10\n[1111111111111111111111000000000000000000000]\tcnt1=11 mswap=10\n[0111111111111111111111100000000000000000000]\tcnt1=11 mswap=10\n[0011111111111111111111110000000000000000000]\tcnt1=11 mswap=10\n[0001111111111111111111111000000000000000000]\tcnt1=11 mswap=10\n[0000111111111111111111111100000000000000000]\tcnt1=11 mswap=10\n[0000011111111111111111111110000000000000000]\tcnt1=11 mswap=10\n[0000001111111111111111111111000000000000000]\tcnt1=11 mswap=10\n[0000000111111111111111111111100000000000000]\tcnt1=11 mswap=10\n[0000000011111111111111111111110000000000000]\tcnt1=11 mswap=10\n[0000000001111111111111111111111000000000000]\tcnt1=11 mswap=10\n[0000000000111111111111111111111100000000000]\tcnt1=11 mswap=10\n[0000000000011111111111111111111110000000000]\tcnt1=11 mswap=10\n[0000000000001111111111111111111111000000000]\tcnt1=11 mswap=10\n[0000000000000111111111111111111111100000000]\tcnt1=11 mswap=10\n[0000000000000011111111111111111111110000000]\tcnt1=11 mswap=10\n[0000000000000001111111111111111111111000000]\tcnt1=11 mswap=10\n[0000000000000000111111111111111111111100000]\tcnt1=11 mswap=10\n[0000000000000000011111111111111111111110000]\tcnt1=11 mswap=10\n[0000000000000000001111111111111111111111000]\tcnt1=11 mswap=10\n[0000000000000000000111111111111111111111100]\tcnt1=11 mswap=10\n[0000000000000000000011111111111111111111110]\tcnt1=11 mswap=10\n[0000000000000000000001111111111111111111111]\tcnt1=11 mswap=10\n```\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code||C++45ms Beats 99.61%\n```\nclass Solution {\npublic:\n    static int  minSwaps(vector<int>& nums) {\n        const int n=nums.size();\n        const int n1=count(nums.begin(), nums.end(), 1);\n        int mswap=INT_MAX, cnt1=0;\n\n        for (int l=0, r=0; r < n+n1; r++) {\n            cnt1+= nums[r%n];\n            if (r-l+1 > n1) \n                cnt1-=nums[l++ % n];\n            \n            if (r-l+1 == n1) \n                mswap = min(mswap, n1-cnt1);\n        }\n        return mswap;\n    }\n};\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n\n```\n# 2nd C++ reducing if-branches||44ms Beats 99.61%\n```\nclass Solution {\npublic:\n    static int minSwaps(vector<int>& nums) {\n        const int n=nums.size();\n        const int n1=count(nums.begin(), nums.end(), 1), n0=n-n1;\n        int mswap;\n        int cnt1=mswap=count(nums.begin(), nums.begin()+n0, 1);\n        for (int l=0, r=n0; r < n+n0; r++, l++) {\n            cnt1+= nums[r%n]-nums[l];\n            mswap=min(mswap, cnt1);//change all to 0 for nums[l+1..r]\n        }\n        return mswap;\n    }\n};\n\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n# Python solution in the video\n```\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        n=len(nums)\n        n1=sum(nums)\n        n0=n-n1\n        mswap=cnt1=sum(nums[0:n0])\n        for l in range(n):\n            r=l+n0\n            cnt1+=nums[r%n]-nums[l]\n            mswap=min(mswap, cnt1)\n        return mswap\n        \n```
['Array', 'Sliding Window', 'C++', 'Python3']