Easy Solution Beats 100%
Sliding Window\nPython []\nclass Solution(object):\n    def minSwaps(self, nums):\n        total = sum(nums)  # Total number of 1\'s in the array\n        n = l
# Sliding Window\n```Python []\nclass Solution(object):\n    def minSwaps(self, nums):\n        total = sum(nums)  # Total number of 1\'s in the array\n        n = len(nums)\n        \n        # If there are no 1\'s, no swaps are needed\n        if total == 0:\n            return 0\n        \n        # Extend the array to handle the circular nature\n        nums *= 2\n        \n        # Initialize the sliding window\n        window_sum = sum(nums[:total])\n        max_ones = window_sum\n        swaps = total - max_ones\n        \n        for i in range(1, n):\n            # Update the number of 1\'s in the current window by sliding the window\n            window_sum += nums[i + total - 1] - nums[i - 1]\n            max_ones = max(max_ones, window_sum)\n            swaps = min(swaps, total - max_ones)\n        \n        return swaps\n```\n```C++ []\nclass Solution {\npublic:\n    int minSwaps(std::vector<int>& nums) {\n        int total = std::accumulate(nums.begin(), nums.end(), 0); // Total number of 1\'s in the array\n        int n = nums.size();\n        \n        // If there are no 1\'s, no swaps are needed\n        if (total == 0) {\n            return 0;\n        }\n        \n        // Extend the array to handle the circular nature\n        nums.insert(nums.end(), nums.begin(), nums.end());\n        \n        // Initialize the sliding window\n        int window_sum = std::accumulate(nums.begin(), nums.begin() + total, 0);\n        int max_ones = window_sum;\n        int swaps = total - max_ones;\n        \n        for (int i = 1; i < n; ++i) {\n            // Update the number of 1\'s in the current window by sliding the window\n            window_sum += nums[i + total - 1] - nums[i - 1];\n            max_ones = std::max(max_ones, window_sum);\n            swaps = std::min(swaps, total - max_ones);\n        }\n        \n        return swaps;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int total = 0;\n        for (int num : nums) {\n            total += num; // Total number of 1\'s in the array\n        }\n        int n = nums.length;\n        \n        // If there are no 1\'s, no swaps are needed\n        if (total == 0) {\n            return 0;\n        }\n        \n        // Extend the array to handle the circular nature\n        int[] extendedNums = new int[n * 2];\n        System.arraycopy(nums, 0, extendedNums, 0, n);\n        System.arraycopy(nums, 0, extendedNums, n, n);\n        \n        // Initialize the sliding window\n        int windowSum = 0;\n        for (int i = 0; i < total; i++) {\n            windowSum += extendedNums[i];\n        }\n        int maxOnes = windowSum;\n        int swaps = total - maxOnes;\n        \n        for (int i = 1; i < n; i++) {\n            // Update the number of 1\'s in the current window by sliding the window\n            windowSum += extendedNums[i + total - 1] - extendedNums[i - 1];\n            maxOnes = Math.max(maxOnes, windowSum);\n            swaps = Math.min(swaps, total - maxOnes);\n        }\n        \n        return swaps;\n    }\n}\n```\n\n# Prefix Sum\n```python []\nclass Solution(object):\n    def minSwaps(self, nums):\n        n = len(nums)\n        total = sum(nums)\n        nums *= 2\n        prefix = [0]\n        for x in nums:\n            prefix.append(prefix[-1] + x)  \n\n        biggest_ones = 0\n        for i in range(n):\n            biggest_ones = max(biggest_ones, prefix[i + total] - prefix[i])\n        return total - biggest_ones         \n```\n```C++ []\nclass Solution {\npublic:\n    int minSwaps(std::vector<int>& nums) {\n        int n = nums.size();\n        int total = std::accumulate(nums.begin(), nums.end(), 0);\n        nums.insert(nums.end(), nums.begin(), nums.end());\n        \n        std::vector<int> prefix(1, 0);\n        for (int x : nums) {\n            prefix.push_back(prefix.back() + x);\n        }\n        \n        int biggest_ones = 0;\n        for (int i = 0; i < n; ++i) {\n            biggest_ones = std::max(biggest_ones, prefix[i + total] - prefix[i]);\n        }\n        \n        return total - biggest_ones;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int n = nums.length;\n        int total = 0;\n        for (int num : nums) {\n            total += num;\n        }\n\n        int[] extendedNums = new int[2 * n];\n        System.arraycopy(nums, 0, extendedNums, 0, n);\n        System.arraycopy(nums, 0, extendedNums, n, n);\n        \n        int[] prefix = new int[2 * n + 1];\n        for (int i = 0; i < 2 * n; i++) {\n            prefix[i + 1] = prefix[i] + extendedNums[i];\n        }\n\n        int biggestOnes = 0;\n        for (int i = 0; i < n; i++) {\n            biggestOnes = Math.max(biggestOnes, prefix[i + total] - prefix[i]);\n        }\n        \n        return total - biggestOnes;\n    }\n}\n\n```\n![unnamed.jpg](https://assets.leetcode.com/users/images/844f564a-aa0c-4f58-a008-c812fc73053f_1722564018.440831.jpeg)\n\n
['Array', 'Sliding Window', 'Prefix Sum', 'Python', 'C++', 'Java']