Slide K Ones Together
bits holds zeros and ones, and at least k of its items are ones. One move makes two items that are next to each other trade places.
Return the fewest moves after which some k ones stand in a row with nothing between them.
Example 1
bits [1,0,1,0,1]
k 2
returns 1
Slide the last 1 one place to the left: 1, 0, 1, 1, 0. The middle two items are now ones in a row.
Example 2
bits [1,0,0,1,0,0,1]
k 3
returns 4
Move the first 1 two places right and the last 1 two places left, so all three meet around the middle one: 4 moves.
Constraints
1 <= len(bits) <= 100- every item is
0or1 1 <= k <=the number of ones inbits
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Spread of Every SelectionHard
- Swaps for Two Rising ListsHard
- Two Trades at MostHard
- Build Array from PermutationBeginner
- Close NeighborsBeginner
- Concatenation of ArrayBeginner
- Deal Into PilesBeginner
- Decompress a Pair ListBeginner