Practice Minimum Moves to Pick K Ones
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Given a binary array nums, k, and maxChanges, choose a fixed index for Alice. If that index initially contains 1, collect it and set it to 0 without spending a move. Each later move either changes a 0 at a different index into 1, or swaps an adjacent 1 and 0. The first type may be used at most maxChanges times. When a swap brings a 1 to Alice’s index, collect it immediately and reset that position to 0. Return the minimum moves, including an optimal choice of starting index, needed to collect exactly k ones.
Source: Minimum Moves to Pick K Ones on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1
3
# Example 2
nums = [0,0,0,0], k = 2, maxChanges = 3
4
Constraints
Related LeetCode Problems
Practice more problems similar to Minimum Moves to Pick K Ones.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.