Swift💯fast🛝🪟
Sliding Window (accepted answer)\n\nclass Solution {\n    func minSwaps(_ nums: [Int]) -> Int {\n        let windowSize = nums.reduce(0,+)\n        var onesInWi
**Sliding Window (accepted answer)**\n```\nclass Solution {\n    func minSwaps(_ nums: [Int]) -> Int {\n        let windowSize = nums.reduce(0,+)\n        var onesInWindow = nums.prefix(windowSize).reduce(0,+)\n        let maxOnesInWindow = nums.indices.reduce(0) { maxOnes, i in\n            onesInWindow -= nums[i] // slide trailing edge\n            onesInWindow += nums[(i+windowSize) % nums.count] // slide leading edge\n            return max(maxOnes, onesInWindow)\n        }\n        return windowSize - maxOnesInWindow\n    }\n}\n```\n1) Create a window (window size = 1\'s count). \n2) Slide window across all elements of array. Determine which window position has max 1\'s present\n3) Subtract max 1\'s from window size and that\'s the minimum swap count to return.\n
['Swift', 'Sliding Window']