Link: https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/
Solution:
Topics: sliding window
Intuition
This is a cute little problem. Lets make the observation that the length of the partition which connects all 1’s is exactly sum(data)
. So the idea is to check every subarray in data
of length sum(data)
to determine the most amount of 1’s that exists in any single partition of that length. The partition with the most amount of 1’s will require the least amount of swaps! We can implement this logic with a running sum.
Implementation
Review 1
Nice problem! While the solution is easy, I felt I had to strain a bit to get to the optimal approach. I’m going to tag this as hard, not because it’s hard but because I think I had to strain a little bit too hard for this one even though I solved it fast.