FastPrepFastPrep
Problem Brief

Minimum Cost to Convert Products to Variant A

FULLTIMEOA
See Amazon online assessment and hiring insights

An inventory array product contains only 0 and 1, where 0 represents variant A and 1 represents variant B.

In one operation, choose a subarray of length k. The cost of that operation is the sum of the values inside the chosen subarray. Then choose one index inside that subarray whose value is 1 and change it to 0.

Return the minimum total cost needed to convert every product to variant A.

Function Description

Complete the function minCostToConvertAllToVariantA in the editor below.

minCostToConvertAllToVariantA has the following parameters:

  1. int[] product: the product variants
  2. int k: the fixed operation window length

Returns

int: the minimum total cost.

1Example 1

Input
product = [1, 1, 1], k = 2
Output
4
Explanation

Use window [0, 1] twice, then window [1, 2] once. The costs are 2 + 1 + 1 = 4.

2Example 2

Input
product = [1, 0, 1], k = 2
Output
2
Explanation

Choose the left window once to clear the first 1, and the right window once to clear the last 1.

Constraints

Limits and guarantees your solution can rely on.

  • product[i] is either 0 or 1
  • 1 <= k <= product.length
  • Each operation must change exactly one 1 inside the chosen window to 0.
public int minCostToConvertAllToVariantA(int[] product, int k) {
    // write your code here
}
Input

product

[1, 1, 1]

k

2

Output

4

Sign in to submit your solution.