Skip to content

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.

Hard#3086
LeetCode Problem

Minimum Moves to Pick K Ones

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

Input
nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1
Output
3

# Example 2

Input
nums = [0,0,0,0], k = 2, maxChanges = 3
Output
4

Constraints

2 <= n <= 10^5
0 <= nums[i] <= 1
1 <= k <= 10^5
0 <= maxChanges <= 10^5
maxChanges + sum(nums) >= k

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.