Minimum Cost to Convert Products to Variant A
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.
Complete the function minCostToConvertAllToVariantA in the editor below.
minCostToConvertAllToVariantA has the following parameters:
int[] product: the product variantsint k: the fixed operation window length
Returns
int: the minimum total cost.
1Example 1
Use window [0, 1] twice, then window [1, 2] once. The costs are 2 + 1 + 1 = 4.
2Example 2
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 either0or11 <= k <= product.length- Each operation must change exactly one
1inside the chosen window to0.