Problem · Array
Minimum Cost to Convert Products to Variant A
Learn this problemProblem statement
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
minCostToConvertAllToVariantA(product: int[], k: int) → intComplete 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.
Examples
Example 1
product = [1, 1, 1]k = 2return = 4Use window [0, 1] twice, then window [1, 2] once. The costs are 2 + 1 + 1 = 4.
Example 2
product = [1, 0, 1]k = 2return = 2Choose the left window once to clear the first 1, and the right window once to clear the last 1.
Constraints
product[i]is either0or11 <= k <= product.length- Each operation must change exactly one
1inside the chosen window to0.