Problem 337043 · medium · Phase 03 Linear Management & Searching

Seat Everyone Together

sliding window · fixed-size window · circular array

A round table has seats described by seats, where 1 means occupied and 0 means empty. In one move any person can swap seats with an empty chair. Return the minimum number of moves needed so that all occupied seats form one contiguous block around the circle (a block may wrap from the last seat to the first).

Examples

Input:  seats = [0, 1, 0, 1, 1, 0, 0]
Output: 1
Explanation: move the person at index 1 to index 5 (or index 2), giving [0,0,0,1,1,1,0].

Input:  seats = [1, 0, 1, 0, 1, 0, 1, 0]
Output: 2
Explanation: four people must end up in a block of 4 seats, and every such block holds two empty chairs.

Input:  seats = [1, 1, 0, 0, 1]
Output: 0
Explanation: the block wraps around: seats 4, 0 and 1 are contiguous on a circle.

Constraints

  • 1 <= len(seats) <= 10**5
  • seats[i] is 0 or 1
  • Target complexity: O(n) time; counting the zeros of each candidate block from scratch is too slow for the largest tests.

Goals

  • Choose the window size from the data itself
  • Handle a circular array by scanning a doubled range without copying it
Starting Python…