1107. Minimum Swaps to Group All 1's Together

Medium
Array
Sliding Window

Description

Hints

Hint 1
How many 1's should be grouped together ? Is not a fixed number?
Hint 2
Yeah it's just the number of 1's the whole array has. Let's name this number as ones
Hint 3
Every subarray of size of ones, needs some number of swaps to reach, Can you find the number of swaps needed to group all 1's in this subarray?
Hint 4
It's the number of zeros in that subarray.
Hint 5
Do you need to count the number of zeros all over again for every position ?
Hint 6
Use Sliding Window technique.

Statistics

Acceptance
61.2%
Submissions
143,621
Accepted
87,853