Class Solution
-
- All Implemented Interfaces:
public final class Solution3086 - Minimum Moves to Pick K Ones\.
Hard
You are given a binary array
numsof lengthn, a positive integerkand a non-negative integermaxChanges.Alice plays a game, where the goal is for Alice to pick up
kones fromnumsusing the minimum number of moves. When the game starts, Alice picks up any indexaliceIndexin the range[0, n - 1]and stands there. Ifnums[aliceIndex] == 1, Alice picks up the one andnums[aliceIndex]becomes0(this does not count as a move). After this, Alice can make any number of moves ( including zero ) where in each move Alice must perform exactly one of the following actions:Select any index
j != aliceIndexsuch thatnums[j] == 0and setnums[j] = 1. This action can be performed at mostmaxChangestimes.Select any two adjacent indices
xandy(|x - y| == 1) such thatnums[x] == 1,nums[y] == 0, then swap their values (setnums[y] = 1andnums[x] = 0). Ify == aliceIndex, Alice picks up the one after this move andnums[y]becomes0.
Return the minimum number of moves required by Alice to pick exactly
kones.Example 1:
Input: nums = 1,1,0,0,0,1,1,0,0,1, k = 3, maxChanges = 1
Output: 3
Explanation: Alice can pick up
3ones in3moves, if Alice performs the following actions in each move when standing ataliceIndex == 1:At the start of the game Alice picks up the one and
nums[1]becomes0.numsbecomes <code>1, **<ins>1</ins>** ,1,0,0,1,1,0,0,1</code>.Select
j == 2and perform an action of the first type.numsbecomes <code>1, **<ins>0</ins>** ,1,0,0,1,1,0,0,1</code>Select
x == 2andy == 1, and perform an action of the second type.numsbecomes <code>1, **<ins>1</ins>** ,0,0,0,1,1,0,0,1</code>. Asy == aliceIndex, Alice picks up the one andnumsbecomes <code>1, **<ins>0</ins>** ,0,0,0,1,1,0,0,1</code>.Select
x == 0andy == 1, and perform an action of the second type.numsbecomes <code>0, **<ins>1</ins>** ,0,0,0,1,1,0,0,1</code>. Asy == aliceIndex, Alice picks up the one andnumsbecomes <code>0, **<ins>0</ins>** ,0,0,0,1,1,0,0,1</code>.
Note that it may be possible for Alice to pick up
3ones using some other sequence of3moves.Example 2:
Input: nums = 0,0,0,0, k = 2, maxChanges = 3
Output: 4
Explanation: Alice can pick up
2ones in4moves, if Alice performs the following actions in each move when standing ataliceIndex == 0:Select
j == 1and perform an action of the first type.numsbecomes <code>**<ins>0</ins>** ,1,0,0</code>.Select
x == 1andy == 0, and perform an action of the second type.numsbecomes <code>**<ins>1</ins>** ,0,0,0</code>. Asy == aliceIndex, Alice picks up the one andnumsbecomes <code>**<ins>0</ins>** ,0,0,0</code>.Select
j == 1again and perform an action of the first type.numsbecomes <code>**<ins>0</ins>** ,1,0,0</code>.Select
x == 1andy == 0again, and perform an action of the second type.numsbecomes <code>**<ins>1</ins>** ,0,0,0</code>. Asy == aliceIndex, Alice picks up the one andnumsbecomes <code>**<ins>0</ins>** ,0,0,0</code>.
Constraints:
<code>2 <= n <= 10<sup>5</sup></code>
0 <= nums[i] <= 1<code>1 <= k <= 10<sup>5</sup></code>
<code>0 <= maxChanges <= 10<sup>5</sup></code>
maxChanges + sum(nums) >= k