💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\n\n\njavascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minSwaps = function (nums) {\n    let n = nums.length, one
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/dfed2152-4d56-4e58-8ef5-84a74ec1507f_1722597985.8682292.jpeg)\n![Screenshot 2024-08-02 162155.png](https://assets.leetcode.com/users/images/5907440e-56a6-432e-a7a7-9cbb77566bf2_1722597995.4010682.png)\n![Screenshot 2024-08-02 163340.png](https://assets.leetcode.com/users/images/792b04c6-775e-43f7-b045-8d7e6c7d4c68_1722598003.4250948.png)\n![Screenshot 2024-08-02 163725.png](https://assets.leetcode.com/users/images/98453a3b-0f3c-4edc-8926-0b21f9853a8f_1722598010.704116.png)\n![Screenshot 2024-08-02 163921.png](https://assets.leetcode.com/users/images/ceb8981e-bc86-4e60-9f8c-b51f9f240eae_1722598017.3022544.png)\n![Screenshot 2024-08-02 164407.png](https://assets.leetcode.com/users/images/487c358f-ad8d-4cb3-8018-616a4a3101ff_1722598024.1637006.png)\n![Screenshot 2024-08-02 165351.png](https://assets.leetcode.com/users/images/2873681a-8aa8-4e24-b16c-c522b6264efc_1722598034.6624513.png)\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minSwaps = function (nums) {\n    let n = nums.length, ones = 0;\n    let circular = Array(2 * n).fill(0);\n    for (let u = 0; u < n; ++u) {\n        circular[u] = circular[u + n] = nums[u];\n        ones += nums[u];\n    }\n    var maxOnes = 0;\n    var currOnes = 0, i = 0, j = 0;\n    for (j = 0; j < ones; ++j)\n        currOnes += circular[j];\n    maxOnes = Math.max(maxOnes, currOnes);\n    while (j < circular.length) {\n        currOnes = currOnes - circular[i] + circular[j];\n        if (maxOnes < currOnes)\n            maxOnes = currOnes;\n        ++j;\n        ++i;\n    }\n\n    return ones - maxOnes;\n};\n```\n```C++ []\n// Start Of Edwards310\'s Template...\n#pragma GCC optimize("O2")\n#include <bits/stdc++.h>\n\nusing namespace std;\n#define FOR(a, b, c) for (int a = b; a < c; a++)\n#define FOR1(a, b, c) for (int a = b; a <= c; ++a)\n#define Rep(i, n) FOR(i, 0, n)\n#define Rep1(i, n) FOR1(i, 1, n)\n#define RepA(ele, nums) for (auto& ele : nums)\n#define WHL(i, n) while (i < n)\n\n// end of Edwards310\'s Template\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int n = nums.size(), ones = 0;\n        vi circular(n * 2, 0);\n        Rep(i, n) {\n            circular[i] = circular[i + n] = nums[i];\n            ones += nums[i];\n        }\n        int maxOnes = 0, currOnes = 0, i = 0, j = 0;\n        for (j = 0; j < ones; ++j)\n            currOnes += circular[j];\n\n        maxOnes = fmax(maxOnes, currOnes);\n        WHL(j, circular.size()) {\n            currOnes = currOnes - circular[i] + circular[j];\n            maxOnes = fmax(maxOnes, currOnes);\n            ++j, ++i;\n        }\n        return ones - maxOnes;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        n, ones = len(nums), 0\n\n        res = [0] * (2 * n)\n        for i in range(n):\n            res[i] = res[i + n] = nums[i]\n            ones += nums[i]\n\n        maxOnes = currOnes = i = j = 0\n\n        while j < ones:\n            currOnes += res[j]\n            j += 1\n\n        maxOnes = max(maxOnes, currOnes)\n        while j < len(res):\n            currOnes = currOnes - res[i] + res[j]\n            maxOnes = max(maxOnes, currOnes)\n            j += 1\n            i += 1\n        return ones - maxOnes\n\n```\n```Java []\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int n = nums.length, ones = 0;\n        int[] circular = new int[n * 2];\n        for (int i = 0; i < n; ++i) {\n            circular[i] = circular[i + n] = nums[i];\n            ones += nums[i];\n        }\n\n        // create a window of size ones\n        int maxOnes = 0, curOnes = 0, i = 0, j = 0;\n        for (j = 0; j < ones; ++j) {\n            curOnes += circular[j];\n        }\n\n        if (maxOnes < curOnes) {\n            maxOnes = curOnes;\n        }\n\n        while (j < circular.length) {\n            curOnes = curOnes - circular[i] + circular[j];\n            if (maxOnes < curOnes)\n                maxOnes = curOnes;\n            ++j;\n            ++i;\n        }\n\n        return ones - maxOnes;\n    }\n}\n```\n```C []\nint minSwaps(int* nums, int numsSize) {\n    int n = numsSize, ones = 0;\n    int circular[2 * n] = {};\n    for (int i = 0; i < n; ++i) {\n        circular[i] = circular[i + n] = nums[i];\n        ones += nums[i];\n    }\n\n    // create a window of size ones\n    int maxOnes = 0, curOnes = 0, i = 0, j = 0;\n    for (j = 0; j < ones; ++j) {\n        curOnes += circular[j];\n    }\n\n    if (maxOnes < curOnes)\n        maxOnes = curOnes;\n\n    while (j < (2 * n)) {\n        curOnes = curOnes - circular[i] + circular[j];\n        if (maxOnes < curOnes)\n            maxOnes = curOnes;\n        ++j;\n        ++i;\n    }\n\n    return ones - maxOnes;\n}\n```\n```Python []\nclass Solution(object):\n    def minSwaps(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        n , ones = len(nums) , 0\n        \n        res = [0] * (2 * n)\n        for i in range(n):\n            res[i] = res[i + n] = nums[i]\n            ones += nums[i]\n            \n        maxOnes = currOnes = i = j = 0\n        \n        while j < ones:\n            currOnes += res[j]\n            j += 1\n            \n        maxOnes = max(maxOnes, currOnes)\n        while j < len(res):\n            currOnes = currOnes - res[i] + res[j]\n            maxOnes = max(maxOnes, currOnes)\n            j += 1\n            i += 1\n        return ones - maxOnes\n```\n```C# []\npublic class Solution {\n    public int MinSwaps(int[] nums) {\n        int n = nums.Length, ones = 0;\n        int[] circular = new int[n * 2];\n        for (int u = 0; u < n; ++u) {\n            circular[u] = circular[u + n] = nums[u];\n            ones += nums[u];\n        }\n        int maxOnes = 0, curOnes = 0, i = 0, j = 0;\n        for (j = 0; j < ones; ++j)\n            curOnes += circular[j];\n        maxOnes = Math.Max(maxOnes, curOnes);\n\n        while (j < circular.Length) {\n            curOnes = curOnes - circular[i] + circular[j];\n            maxOnes = Math.Max(maxOnes, curOnes);\n            ++j;\n            ++i;\n        }\n\n        return ones - maxOnes;\n    }\n}\n```\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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(2 * n)\n# Code\n```\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        n , ones = len(nums) , 0\n        \n        res = [0] * (2 * n)\n        for i in range(n):\n            res[i] = res[i + n] = nums[i]\n            ones += nums[i]\n            \n        maxOnes = currOnes = i = j = 0\n        \n        while j < ones:\n            currOnes += res[j]\n            j += 1\n            \n        maxOnes = max(maxOnes, currOnes)\n        while j < len(res):\n            currOnes = currOnes - res[i] + res[j]\n            maxOnes = max(maxOnes, currOnes)\n            j += 1\n            i += 1\n        return ones - maxOnes\n```\n![th.jpeg](https://assets.leetcode.com/users/images/86b382e4-8a8f-45dc-9b0c-1dc2831ea049_1722598393.922112.jpeg)\n
['Array', 'C', 'Sliding Window', 'Python', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript', 'C#']