↓Skip to main content
  1. LeetCode/

LeetCode 2134: Minimum Swaps to Group All 1's Together II

·2 mins· ·
LeetCode Medium Sliding-Window Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-12-27

  • Submissions: Accepted (first try)

Approach
#

Let ones be the number of 1s. Once grouped, the 1s occupy ones consecutive positions on the circle.

For a given window, each 0 inside must swap with a 1 outside, one per swap, so swaps = the number of 0s in the window.

Slide a window of length ones around the circle (indices % n) and find the one with the fewest 0s.

My Solution
#

My accepted version avoided the circle with another view: on a circle “the 1s are together” looks like one of two things on a line:

  • The 1s are contiguous on the line: count 0s in length-ones windows.
  • The 1s wrap around the ends: then the 0s are contiguous on the line, so count 1s in length-zeros windows.

One fixed window each, taking the minimum. Here it is tidied into a direct circular slide:

class Solution:
    def minSwaps(self, nums: List[int]) -> int:
        n = len(nums)
        ones = sum(nums)
        zeros_in = ones - sum(nums[:ones])         # 0s in window [0, ones)
        best = zeros_in
        for right in range(ones, n + ones - 1):    # window starts 1..n-1
            zeros_in += (nums[right % n] == 0) - (nums[(right - ones) % n] == 0)
            best = min(best, zeros_in)
        return best

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Forgetting the circle: the 1s may wrap around the ends.
  • Swaps are not “distance moved”: any two positions can be swapped, so one swap removes one 0.

Interview Explanation
#

Once grouped, the 1s take a stretch as long as their count, and every 0 inside must swap once with a 1 outside, so the swaps equal the 0s in the window. I slide a fixed window around the circle using indices mod n and take the fewest 0s. Without the circle: when the 1s wrap around, the 0s are contiguous on the line, so look for the length-zeros window with the fewest 1s. Time O(n).

Related

LeetCode 1423: Maximum Points You Can Obtain from Cards
·1 min
LeetCode Medium Sliding-Window Prefix-Sum Array
LeetCode 1423 note: taking k cards from the ends leaves a contiguous middle of n - k cards; find its minimum sum with a fixed-size window
LeetCode 2461: Maximum Sum of Distinct Subarrays With Length K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2461 note: in a fixed window of length k, all values are distinct exactly when the number of distinct values equals k
LeetCode 1493: Longest Subarray of 1's After Deleting One Element
·2 mins
LeetCode Medium Sliding-Window Dynamic-Programming Array
LeetCode 1493 note: find the longest window with at most one 0; since one element must be deleted, the answer is the window length minus 1
LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave
LeetCode 2841: Maximum Sum of Almost Unique Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2841 note: a fixed window of length k keeps a sum and a count map; the number of distinct values is the map’s size, so delete keys that drop to zero
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map