#3086
Minimum Moves to Pick K Ones
grandmaster · 2200 · lc hard +32 · premium · 21.5% accepted · 61 likes · top 2%
Description
Given a binary array nums, a positive integer k, and a non-negative integer maxChanges, Alice starts at any chosen index aliceIndex and picks up the 1 there for free (if present). Each subsequent move is one of:
- Set any nums[j] = 1 (at most maxChanges times total).
- Swap adjacent nums[x] == 1 and nums[y] == 0; if y == aliceIndex, Alice collects the 1.
Return the minimum number of moves for Alice to collect exactly k ones.
Code
1
2
3