Minimum Cost to Partition a Binary String
You are given a binary string s, and two cost multipliers: Cost1 and Cost2. '1' means sensitive, '0' means non-sensitive.
For any segment of length L containing X sensitive elements:
- If X == 0, the cost is Cost2.
- If X > 0, the cost is L * X * Cost1.
You start with the entire string as one segment. If a segment has an EVEN length, you are allowed to split it perfectly in half. The cost is then the sum of the two halves.
Return the absolute minimum cost achievable by splitting (or choosing not to split) the string. Your task is to Determine the optimal stopping points for splitting to minimize mathematical cost.
Example 1:
Input: s = "1010", Cost1 = 2, Cost2 = 1
Output: 6
Explanation:
- Splitting "1010" down to "1", "0", "1", "0" gives costs of 2, 1, 2, 1 respectively. Sum = 6.
Example 2:
Input: s = "1010", Cost1 = 3, Cost2 = 10
Output: 12
Explanation:
- Entire string cost: 4 * 2 * 3 = 24.
- Split into "10" and "10": each costs 2 * 1 * 3 = 6. Total = 12.
- Split again into "1", "0": "1" costs 1 * 1 * 3 = 3. "0" costs flatCost = 10. Total = 3 + 10 + 3 + 10 = 26.
The minimum cost is 12 (halting the split at "10").
Now Your Turn!
Pick the correct output for the given inputInput: s = "110", Cost1 = 2, Cost2 = 1
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= s.length <= 105
- s consists only of '0' and '1'.
- 1 <= Cost1, Cost2 <= 105