Skip to content

Practice Maximize Active Section with Trade II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3501
LeetCode Problem

Maximize Active Section with Trade II

Problem Statement

The binary string s uses 1 for active sections and 0 for inactive ones. For each inclusive query [l, r] independently, you may perform at most one trade entirely inside that substring: turn a run of ones surrounded by zeroes into zeroes, then turn a run of zeroes surrounded by ones into ones. Treat the substring as having a virtual 1 at each end when checking these conditions. Return the maximum number of ones in the whole string s after that query’s trade; positions outside the query remain unchanged and virtual positions are not counted.

Source: Maximize Active Section with Trade II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "01", queries = [[0,1]]
Output
[1]

# Example 2

Input
s = "0100", queries = [[0,3],[0,2],[1,3],[2,3]]
Output
[4,3,1,1]

# Example 3

Input
s = "1000100", queries = [[1,5],[0,6],[0,4]]
Output
[6,7,2]

# Example 4

Input
s = "01010", queries = [[0,3],[1,4],[1,3]]
Output
[4,4,2]

Constraints

1 <= n == s.length <= 10^5
1 <= queries.length <= 10^5
s[i] is either '0' or '1'.
queries[i] = [li, ri]
0 <= li <= ri < n

Related LeetCode Problems

Practice more problems similar to Maximize Active Section with Trade II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.