3864. Minimum Cost to Partition a Binary String Hard
1/**
2 * [3864] Minimum Cost to Partition a Binary String
3 *
4 * You are given a binary string s and two integers encCost and flatCost.
5 * For each index i, s[i] = '1' indicates that the i^th element is sensitive, and s[i] = '0' indicates that it is not.
6 * The string must be partitioned into segments. Initially, the entire string forms a single segment.
7 * For a segment of length L containing X sensitive elements:
8 *
9 * If X = 0, the cost is flatCost.
10 * If X > 0, the cost is L * X * encCost.
11 *
12 * 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.
13 * Return an integer denoting the minimum possible total cost over all valid partitions.
14 *
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">s = "1010", encCost = 2, flatCost = 1</span>
18 * Output: <span class="example-io">6</span>
19 * Explanation:
20 *
21 * The entire string s = "1010" has length 4 and contains 2 sensitive elements, giving a cost of 4 * 2 * 2 = 16.
22 * 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.
23 * 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.
24 * The total cost is thus 2 + 1 + 2 + 1 = 6, which is the minimum possible total cost.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">s = "1010", encCost = 3, flatCost = 10</span>
29 * Output: <span class="example-io">12</span>
30 * Explanation:
31 *
32 * The entire string s = "1010" has length 4 and contains 2 sensitive elements, giving a cost of 4 * 2 * 3 = 24.
33 * Since the length is even, it can be split into two segments "10" and "10".
34 * 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.
35 * </div>
36 * <strong class="example">Example 3:
37 * <div class="example-block">
38 * Input: <span class="example-io">s = "00", encCost = 1, flatCost = 2</span>
39 * Output: <span class="example-io">2</span>
40 * Explanation:
41 * 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.
42 * </div>
43 *
44 * Constraints:
45 *
46 * 1 <= s.length <= 10^5
47 * s consists only of '0' and '1'.
48 * 1 <= encCost, flatCost <= 10^5
49 *
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/minimum-cost-to-partition-a-binary-string/
54// discuss: https://leetcode.com/problems/minimum-cost-to-partition-a-binary-string/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59 pub fn min_cost(s: String, enc_cost: i32, flat_cost: i32) -> i64 {
60
61 }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_3864() {
72 }
73}
74Back
© 2026 bowen.ge All Rights Reserved.