❇ Crisp n Clear👌 🏆O(N)❤️ Javascript❤️ Runtime👀90%🕕 Meaningful Vars✍️ 🔴❇ ✅ 👉 💪🙏
Intuition\n Describe your first thoughts on how to solve this problem. \nCount how many total ones are there(totalOnes)\n\n# Approach\n Describe your approach t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCount how many total ones are there(totalOnes)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nselect first initial block and how many ones are there\nfind a block of totalOnes length which has maxOnesInBlock\nreturn totalOnes - maxOnesInBlock;\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nvar minSwaps = function (nums) {\n    let totalOnes = nums.reduce(\n        (accumulator, value) => (value && ++accumulator) || accumulator,\n        0\n    );\n    const block = nums.slice(0, totalOnes);\n    let totalOnesInBlock = block.reduce(\n        (accumulator, value) => (value && ++accumulator) || accumulator,\n        0\n    );\n    let maxOnesInBlock = totalOnesInBlock;\n    for (let index = 0; index < nums.length; index++) {\n        if (nums[(index + totalOnes) % nums.length] === 1) {\n            totalOnesInBlock += 1;\n        }\n        if (nums[index % nums.length] === 1) {\n            totalOnesInBlock -= 1;\n        }\n        if (maxOnesInBlock < totalOnesInBlock) {\n            maxOnesInBlock = totalOnesInBlock;\n        }\n    }\n    return totalOnes - maxOnesInBlock;\n};\n```
['JavaScript']