Minimum Cost to Partition a Binary String Problem
Description
LeetCode Problem 3864.
You are given a binary string s and two integers encCost and flatCost.
For each index i, s[i] = '1' indicates that the i^th element is sensitive, and s[i] = '0' indicates that it is not.
The string must be partitioned into segments. Initially, the entire string forms a single segment.
For a segment of length L containing X sensitive elements:
- If
X = 0, the cost isflatCost. - If
X > 0, the cost isL * X * encCost.
If a segment has even length, you may split it into two contiguous segments of equal length and the cost of this split is the sum of costs of the resulting segments.
Return an integer denoting the minimum possible total cost over all valid partitions.
Example 1:
Input: s = "1010", encCost = 2, flatCost = 1
Output: 6
Explanation:
- The entire string s = "1010" has length 4 and contains 2 sensitive elements, giving a cost of 4 * 2 * 2 = 16.
- Since the length is even, it can be split into "10" and "10". Each segment has length 2 and contains 1 sensitive element, so each costs 2 * 1 * 2 = 4, giving a total of 8.
- Splitting both segments into four single-character segments yields the segments "1", "0", "1", and "0". A segment containing "1" has length 1 and exactly one sensitive element, giving a cost of 1 * 1 * 2 = 2, while a segment containing "0" has no sensitive elements and therefore costs flatCost = 1.
- The total cost is thus 2 + 1 + 2 + 1 = 6, which is the minimum possible total cost.
Example 2:
Input: s = "1010", encCost = 3, flatCost = 10
Output: 12
Explanation:
- The entire string s = "1010" has length 4 and contains 2 sensitive elements, giving a cost of 4 * 2 * 3 = 24.
- Since the length is even, it can be split into two segments "10" and "10".
- Each segment has length 2 and contains one sensitive element, so each costs 2 * 1 * 3 = 6, giving a total of 12, which is the minimum possible total cost.
Example 3:
Input: s = "00", encCost = 1, flatCost = 2
Output: 2
Explanation:
The string s = "00" has length 2 and contains no sensitive elements, so storing it as a single segment costs flatCost = 2, which is the minimum possible total cost.
Constraints:
1 <= s.length <= 10^5sconsists only of'0'and'1'.1 <= encCost, flatCost <= 10^5
Solution
Approach: Recursion over Halving
A segment can only be cut in half, so every segment that can appear is a block of an implicit binary tree over the string. Build prefix sums of the ones, and let dfs(l, r) be the cheapest cost of the block [l, r). Its unsplit cost is flatCost if it has no ones and (r - l) * x * encCost otherwise (x = number of ones). If the length is even we may also split it in the middle and pay dfs(l, m) + dfs(m, r); take the smaller. The answer is dfs(0, n). Each level of the tree covers the string once, and the number of blocks is at most 2n.
Complexity: O(n) time and O(n) space (prefix sums; the recursion depth is only O(log n)).
class Solution:
def minCost(self, s: str, encCost: int, flatCost: int) -> int:
def dfs(l: int, r: int) -> int:
x = pre[r] - pre[l]
res = (r - l) * x * encCost if x else flatCost
if (r - l) % 2 == 0:
m = (l + r) // 2
res = min(res, dfs(l, m) + dfs(m, r))
return res
n = len(s)
pre = [0] * (n + 1)
for i, c in enumerate(s, 1):
pre[i] = pre[i - 1] + int(c)
return dfs(0, n)