← Questions

Minimum cost to clear a binary array with k-wide windows

0
AmazonCoding

A coding interview question asked at Amazon. It tests greedy, sliding window and math.

A packaging line tracks a row of n units, each built as one of two variants: variant A is written 0 and variant B is written 1. You need to convert every unit to variant A.

You are given the binary array product and an integer k (1 <= k <= n). One operation works like this:

  1. Pick a contiguous block of exactly k positions, [l, l + k - 1].
  2. Pay the current sum of product over that block, measured before anything changes.
  3. Pick one position in the block that holds a 1 and set it to 0. The block must contain at least one 1.

Return the minimum total cost to make every element 0.

def min_conversion_cost(product: list[int], k: int) -> int: ...

Constraints

  • 1 <= n <= 2 * 10^5
  • product[i] is 0 or 1
  • 1 <= k <= n
  • The answer can exceed 32 bits.

Example 1

product = [1, 1, 1], k = 2
output  = 4

Choose block [0, 1] (cost 2) and clear index 1, which gives [1, 0, 1]. Now block [0, 1] costs 1 to clear index 0, and block [1, 2] costs 1 to clear index 2. The total is 4. Clearing index 0 first would cost 2 + 2 + 1 = 5.

Example 2

product = [0, 1, 1, 1, 1, 0], k = 4
output  = 7

Every block of length 4 holds at least 3 ones. Clearing the three ones of block [0, 3] while paying for that block each time costs 3 + 2 + 1. The last 1 then costs 1.

Part 2 (if time allows): return one optimal sequence of operations as (l, index_cleared, cost) triples.

Clarifying questions

  • Is the cost measured on the block before the chosen 1 is cleared, so a block with one 1 costs 1?
  • If the array is already all zeros, is the answer 0?
  • Can the same block be chosen again after some of its ones are cleared?
  • For k = n, is the answer m + (m - 1) + ... + 1 for m ones, as a sanity check?

What a strong answer covers

  • States the cost of each clear as the number of ones still present in the cheapest block that contains it, so the problem is choosing the order of clears and the block for each.
  • Reasons about the process in reverse (start from all zeros and put the ones back), where each one costs the count of ones already back in its best block plus itself; this makes a greedy choice easier to state and prove.
  • Builds an exhaustive search over clear orders for small arrays and checks any proposed greedy against it, including both examples.
  • Keeps block sums current with a Fenwick tree or a segment tree so each step costs O(log n), and uses 64-bit totals.
  • Tests k = 1 (every one costs 1), k = n, and ones in clusters far apart.

Follow-ups

  • Part 2: return the sequence of (l, index_cleared, cost) operations.
  • Let the operation clear every one in the block at once, paying the block's sum once.
  • Change the price to the block's sum after the clear.

Where it's asked

What it tests

greedy · sliding window · math

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding