Maximize Active Section with Trade II - Problem
Maximize Active Section with Trade II

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. A trade involves:
1. Converting a contiguous block of '1's surrounded by '0's to all '0's
2. Afterward, converting a contiguous block of '0's surrounded by '1's to all '1's

You are also given a 2D array queries, where queries[i] = [l_i, r_i] represents a substring s[l_i...r_i]. For each query, determine the maximum possible number of active sections after making the optimal trade on that substring.

Important: For each query, the substring s[l_i...r_i] is treated as if it's augmented with a '1' at both ends: t = '1' + s[l_i...r_i] + '1'. These augmented '1's don't contribute to the final count.

Return an array answer where answer[i] is the result for queries[i].

Input & Output

example_1.py โ€” Basic Trade
$ Input: s = "10111001011", queries = [[0, 10]]
โ€บ Output: [9]
๐Ÿ’ก Note: For the entire string with padding '1|10111001011|1', we can trade the isolated '1' at position 1 (lose 1) to gain the '0' block at positions 6-7 (gain 2), resulting in net +1. But better: trade the '1' at position 1 for the '010' block at positions 6-8 (net +2). Total: 7 original + 2 = 9.
example_2.py โ€” Multiple Queries
$ Input: s = "1100101", queries = [[0, 6], [2, 5]]
โ€บ Output: [5, 4]
๐Ÿ’ก Note: Query 1: Full string '1|1100101|1' โ†’ can trade '1' block (size 1) for '0' block (size 2), net +1, total 4+1=5. Query 2: Substring '1|0101|1' โ†’ can trade nothing beneficial, stays 2. Wait, let me recalculate: substring has 2 ones, no beneficial trades, result is 2.
example_3.py โ€” No Beneficial Trade
$ Input: s = "111000", queries = [[0, 5]]
โ€บ Output: [3]
๐Ÿ’ก Note: With padding '1|111000|1', there are no isolated blocks to trade (the '111' is not surrounded by '0's on both sides, nor is '000' surrounded by '1's on both sides). Original count of 3 remains unchanged.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 105
  • 0 โ‰ค li โ‰ค ri < n
  • s consists only of '0' and '1'
  • Each query is independent

Visualization

Tap to expand
Power Grid: 1|0111001011|11Small Island111Large Island0Small Valley010Large ValleyOptimal Trade: -1 + 3 = +2Result: 7 original stations + 2 net gain = 9 total powered sections
Understanding the Visualization
1
Identify Islands & Valleys
Scan the grid to find isolated powered islands ('1' blocks surrounded by '0's) and unpowered valleys ('0' blocks surrounded by '1's)
2
Find Optimal Trade
Choose the smallest powered island to shut down and the largest unpowered valley to activate
3
Calculate Net Benefit
If (valley size - island size) > 0, make the trade for a net gain
4
Apply Strategy
Add the net benefit to the original power count
Key Takeaway
๐ŸŽฏ Key Insight: The optimal strategy always involves the smallest sacrifice for the largest gain, making this a classic greedy optimization problem.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen