3499. Maximize Active Section with Trade I Medium
1/**
2 * [3499] Maximize Active Section with Trade I
3 *
4 * You are given a binary string s of length n, where:
5 *
6 * '1' represents an active section.
7 * '0' represents an inactive section.
8 *
9 * You can perform at most one trade to maximize the number of active sections in s. In a trade, you:
10 *
11 * Convert a contiguous block of '1's that is surrounded by '0's to all '0's.
12 * Afterward, convert a contiguous block of '0's that is surrounded by '1's to all '1's.
13 *
14 * Return the maximum number of active sections in s after making the optimal trade.
15 * Note: Treat s as if it is augmented with a '1' at both ends, forming t = '1' + s + '1'. The augmented '1's do not contribute to the final count.
16 *
17 * <strong class="example">Example 1:
18 * <div class="example-block">
19 * Input: <span class="example-io">s = "01"</span>
20 * Output: <span class="example-io">1</span>
21 * Explanation:
22 * Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 1.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">s = "0100"</span>
27 * Output: <span class="example-io">4</span>
28 * Explanation:
29 *
30 * String "0100" → Augmented to "101001".
31 * Choose "0100", convert "10<u>1</u>001" → "1<u>0000</u>1" → "1<u>1111</u>1".
32 * The final string without augmentation is "1111". The maximum number of active sections is 4.
33 * </div>
34 * <strong class="example">Example 3:
35 * <div class="example-block">
36 * Input: <span class="example-io">s = "1000100"</span>
37 * Output: <span class="example-io">7</span>
38 * Explanation:
39 *
40 * String "1000100" → Augmented to "110001001".
41 * Choose "000100", convert "11000<u>1</u>001" → "11<u>000000</u>1" → "11<u>111111</u>1".
42 * The final string without augmentation is "1111111". The maximum number of active sections is 7.
43 * </div>
44 * <strong class="example">Example 4:
45 * <div class="example-block">
46 * Input: <span class="example-io">s = "01010"</span>
47 * Output: <span class="example-io">4</span>
48 * Explanation:
49 *
50 * String "01010" → Augmented to "1010101".
51 * Choose "010", convert "10<u>1</u>0101" → "1<u>000</u>101" → "1<u>111</u>101".
52 * The final string without augmentation is "11110". The maximum number of active sections is 4.
53 * </div>
54 *
55 * Constraints:
56 *
57 * 1 <= n == s.length <= 10^5
58 * s[i] is either '0' or '1'
59 *
60 */
61pub struct Solution {}
62
63// problem: https://leetcode.com/problems/maximize-active-section-with-trade-i/
64// discuss: https://leetcode.com/problems/maximize-active-section-with-trade-i/discuss/?currentPage=1&orderBy=most_votes&query=
65
66// submission codes start here
67
68impl Solution {
69 pub fn max_active_sections_after_trade(s: String) -> i32 {
70 0
71 }
72}
73
74// submission codes end
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn test_3499() {
82 }
83}
84Back
© 2026 bowen.ge All Rights Reserved.