Maximize Active Section with Trade II - Problem
Maximize Active Section with Trade II
You are given a binary string
โข
โข
You can perform at most one trade to maximize the number of active sections. A trade involves:
1. Converting a contiguous block of
2. Afterward, converting a contiguous block of
You are also given a 2D array
Important: For each query, the substring
Return an array
You are given a binary string
s of length n, where:โข
'1' represents an active sectionโข
'0' represents an inactive sectionYou 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's2. Afterward, converting a contiguous block of
'0's surrounded by '1's to all '1'sYou 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code