Minimum Swaps to Group All 1's Together II
MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.You get a circular binary array nums: the last element is next to the first. A swap exchanges the values at any two positions. Return the minimum number of swaps needed to put all the 1s next to each other somewhere around the circle.
nums = [0, 1, 0, 1, 1, 0, 0] → 1
nums = [0, 1, 1, 1, 0, 0, 1, 1, 0] → 2
nums = [1, 1, 0, 0, 1] → 0 (wraps around: 1, 1, 1)Constraints
1 <= nums.length <= 10^5nums[i]is either0or1.