2134. Minimum Swaps to Group All 1's Together II
2134. Minimum Swaps to Group All 1’s Together II
Description
A swap is defined as taking two distinct positions in an array and swapping the values in them.
A circular array is defined as an array where we consider the first element and the last element to be adjacent .
Given a binary circular array nums
, return the minimum number of swaps required to group all 1
‘s present in the array together at any location .
Example 1:
1 | Input: nums = [0,1,0,1,1,0,0] |
Example 2:
1 | Input: nums = [0,1,1,1,0,0,1,1,0] |
Example 3:
1 | Input: nums = [1,1,0,0,1] |
Constraints:
1 <= nums.length <= 10^5
nums[i]
is either0
or1
.
Hints/Notes
- 2024/10/24
- sliding window
- No solution from 0x3F
Solution
Language: C++
1 | class Solution { |