Watch 6 video solutions for Maximize Active Section with Trade I, a medium level problem involving String, Enumeration. This walkthrough by Rapid Syntax has 1,746 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a binary string s of length n, where:
'1' represents an active section.'0' represents an inactive section.You can perform at most one trade to maximize the number of active sections in s. In a trade, you:
'1's that is surrounded by '0's to all '0's.'0's that is surrounded by '1's to all '1's.Return the maximum number of active sections in s after making the optimal trade.
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.
Example 1:
Input: s = "01"
Output: 1
Explanation:
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.
Example 2:
Input: s = "0100"
Output: 4
Explanation:
"0100" → Augmented to "101001"."0100", convert "101001" → "100001" → "111111"."1111". The maximum number of active sections is 4.Example 3:
Input: s = "1000100"
Output: 7
Explanation:
"1000100" → Augmented to "110001001"."000100", convert "110001001" → "110000001" → "111111111"."1111111". The maximum number of active sections is 7.Example 4:
Input: s = "01010"
Output: 4
Explanation:
"01010" → Augmented to "1010101"."010", convert "1010101" → "1000101" → "1111101"."11110". The maximum number of active sections is 4.
Constraints:
1 <= n == s.length <= 105s[i] is either '0' or '1'Problem Overview: You are given a binary string where '1' represents an active slot and '0' represents inactive. You can perform at most one trade (swap a '0' with a '1'). The goal is to maximize the length of a contiguous active section after the trade.
Approach 1: Brute Force Swap Enumeration (O(n²) time, O(1) space)
Enumerate every pair of indices where a '0' and a '1' can be swapped. After each swap, scan the string to compute the longest contiguous block of '1'. Track the maximum length observed. This approach directly simulates the allowed operation but becomes expensive because each potential swap requires another linear scan. It works for small inputs and is useful for verifying correctness of optimized approaches.
Approach 2: Enumerating Zero Positions (O(n) time, O(1) space)
Instead of simulating every swap, focus on positions of '0'. A single trade effectively turns one '0' inside a region into '1', potentially connecting two adjacent blocks of '1'. For each zero index, count the length of consecutive '1' on the left and right. The combined size represents the merged segment if that zero becomes '1'. Cap the result by the total number of '1' in the string because a swap cannot create new active slots. This linear scan strategy uses simple counting and works well when reasoning about segment merging.
Approach 3: Greedy Sliding Window (Two Pointers) (O(n) time, O(1) space)
The optimal solution treats the trade as allowing at most one '0' inside the active window. Use a sliding window with two pointers over the string. Expand the right pointer while tracking how many zeros are inside the window. If the window contains more than one zero, move the left pointer until the constraint is restored. The window length represents the longest segment achievable if one zero in the window is swapped with a '1' outside. Finally, limit the answer by the total count of '1'. This greedy approach processes each character once and naturally fits problems involving two pointers and local window constraints.
Recommended for interviews: The greedy sliding window solution is what interviewers expect. It shows you can translate a "single modification" constraint into a window condition and solve it in linear time. Discussing the brute force or zero-enumeration idea first demonstrates problem understanding, while the enumeration and two‑pointer optimization shows strong algorithmic reasoning.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Swap Enumeration | O(n²) | O(1) | Small inputs or for validating optimized solutions |
| Enumerate Zero Positions | O(n) | O(1) | When reasoning about merging adjacent blocks of 1s |
| Greedy Sliding Window (Two Pointers) | O(n) | O(1) | Best general solution for large inputs and interview settings |