Easy Sliding Window Solution | Golang
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can use a sliding window to check the number of ones missing from each interval. The
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can use a sliding window to check the number of ones missing from each interval. The fact that the list is circular, means that we need to ensure we check the window starting before index `0`.\n\nThe size of the window, $$k$$, is the number of ones in the array. In any given window of length $$k$$, the number of zeroes in the window is the number of swaps needed. \n\nThen we only need to check each window, remembering the minimum number of swaps to return. \n \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count and store the number of `ones` in `nums` - this will define the length of the window\n2. For the final `ones` in `nums`, count the `swaps` needed, and set `min_swaps` equal to `swaps`\n3. Starting at index `0`\n   - If the current index is a zero, increment `swaps`\n   - Set the previous index at `ones` places behind `i`, adding `len(nums)` if it is below zero\n   - If the previous index is a zero, decrement `swaps`\n   - If `swaps` is less than `min_swaps`, set the new minimum\n4. Return `min_swaps`\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g.  -->\nCounting ones is n-time, and setting the initial window is k-time, where k is the total number of ones. Checking each window is n-time. So the total time complexity is $$O(2n + k)$$, which simplifies to $$O(n)$$.\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThere is no need for additional data structures, so space complexity is constant.\n\n# Code\n```\nfunc minSwaps(nums []int) int {\n    ones := 0\n    for _, num := range nums {\n        if num == 1 {\n            ones++\n        }\n    }\n    \n    swaps := 0\n    for i := len(nums)-ones; i < len(nums); i++ {\n        if nums[i] == 0 {\n            swaps += 1\n        }\n    }\n    \n    min_swaps := swaps\n    for i := 0; i < len(nums); i++ {\n        if nums[i] == 0 {\n            swaps += 1\n        }\n        last := i-ones\n        if last < 0 {\n            last += len(nums)\n        }\n        if nums[last] == 0 {\n            swaps -= 1\n        }\n        if swaps < min_swaps {\n            min_swaps = swaps\n        }\n    }\n    return min_swaps\n}\n```
['Array', 'Sliding Window', 'Go']