✅💪🔥 Simplest Python code || Sliding Window method || Beats 93% 🔥 || Easy understanding
Intuition\nAdd the same array and use sliding window approach.\n\n# Approach\n- Count the total number of 1\'s: We count how many 1\'s are in the original array
# Intuition\nAdd the same array and use sliding window approach.\n\n# Approach\n- **Count the total number of 1\'s**: We count how many 1\'s are in the original array. This count determines the size of the window we need to consider for grouping all 1\'s together. **Edge case :** If the count is equal to the length of the array or if the number of ones are zero then return 0 swaps.\n\n- **Extend the array**: To handle the circular nature of the array, we extend the array by appending it to itself. This way, we can easily simulate the circular behavior using a simple linear traversal.\n\n```\n        total_ones = nums.count(1)\n        if total_ones == 0 or total_ones == len(nums):\n            return 0\n        extended_nums = nums + nums\n```\n\n- **Initialize the sliding window:** We start by calculating the number of 1\'s in the first window (of size equal to the total number of 1\'s).\n\n- **Slide the window:** We slide the window across the extended array and keep track of the maximum number of 1\'s found in any window of the given size.\n\n```\n        current_ones = sum(extended_nums[:total_ones])\n        max_ones = current_ones\n        for i in range(1, len(nums)):\n            current_ones = current_ones - extended_nums[i - 1] +   extended_nums[i + total_ones - 1]\n            max_ones = max(max_ones, current_ones)\n```\n\n- **Calculate the minimum swaps:** The minimum number of swaps required to group all 1\'s together is the total number of 1\'s minus the maximum number of 1\'s found in any window.\n\n```\n        min_swaps = total_ones - max_ones\n        return min_swaps\n```\n\n# Complexity\n- Time complexity:\n`O(N)`\n\n- Space complexity:\n`O(N)`\n\n\n![Screenshot 2024-08-02 094615.jpg](https://assets.leetcode.com/users/images/d36b9ac1-e337-4ac8-8883-680c8cd9611b_1722572190.1766334.jpeg)\n\n\n\n# Code\n```\nclass Solution(object):\n    def minSwaps(self, nums):\n        total_ones = nums.count(1)\n        if total_ones == 0 or total_ones == len(nums):\n            return 0\n        extended_nums = nums + nums\n        current_ones = sum(extended_nums[:total_ones])\n        max_ones = current_ones\n        for i in range(1, len(nums)):\n            current_ones = current_ones - extended_nums[i - 1] + extended_nums[i + total_ones - 1]\n            max_ones = max(max_ones, current_ones)\n        min_swaps = total_ones - max_ones\n        return min_swaps\n\n        \n```
['Array', 'Sliding Window', 'Python']