Sliding Window O(n) switch JavaScript Solution
IntuitionApproachComplexity
Time complexity:
O(n)
Space complexity:
O(1)Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)

# Code
```javascript []
/**
 * @param {number[]} nums
 * @return {number}
 */
var minSwaps = function(nums) {
    //count of 1's item
    // will be the size of the sliding window
    let cnt = nums.reduce((acc, curr) => acc+curr , 0)
    let len = nums.length


    //get the first sliding window (start point) with same legth of number of 1's appearance count
    //get it 1's count existance
    let slide = nums.slice(0, cnt).reduce((acc, curr) => acc+curr , 0)

    //will be minues from **cnt** to get the number of 0's
    let max = slide
    
    for(let i=0 ; i<len ; i++){
        //extend array by using MOD (%)
        //extend sliding window by one
        if(nums[(cnt+i)%len] === 1){
            slide++
        }
        //remove one item from the begin of window as moving
        if(nums[i] === 1){
            slide--
        }
        //the max number of 1's exist =====> minimize the number of 0's exist of window
        max = Math.max(max, slide)
    }
    return cnt - max
};
```
['Array', 'Sliding Window', 'JavaScript']