Intuition¶
A valid trade removes some internal block of '1's (turning them into '0's)
and then flips a larger block of '0's into '1's. That flip merges two adjacent
runs of zeros that sit on either side of a '1'-run, so the net gain equals the
sum of those two zero-run lengths (the removed ones become ones again as part of
the flipped block).
Without a trade the answer is just the number of '1's. With one trade, add the
largest sum of any two consecutive zero-runs in s.
Approach: Scan Consecutive Zero Runs¶
- Count total
'1's ins. - Walk the string by runs of equal characters.
- For each run of
'0's of lengthcurrent, if a previous zero-runprevexists, updatebest = max(best, prev + current). - Set
prev = currentand continue. - Return
countOne + best(if no pair of zero-runs exists,beststays 0).
Complexity¶
- Time complexity: $$O(n)$$, where
niss.length— one linear scan. - Space complexity: $$O(1)$$ extra space.
Code¶
Go¶
func maxActiveSectionsAfterTrade(s string) int {
n := len(s)
countOne := 0
for _, ch := range s {
if ch == '1' {
countOne++
}
}
best := 0
for prev, current, i := -1, 0, 0; i < n; {
start := i
for i < n && s[i] == s[start] {
i++
}
if s[start] == '0' {
current = i - start
if prev != -1 {
best = max(best, prev+current)
}
prev = current
}
}
return countOne + best
}
Rust¶
impl Solution {
pub fn max_active_sections_after_trade(s: String) -> i32 {
let count_one = s.bytes().filter(|&c| c == b'1').count() as i32;
let s = s.as_bytes();
let n = s.len();
let mut best = 0;
let mut prev = -1;
let mut current = 0;
let mut i = 0;
while i < n {
let start = i;
while i < n && s[i] == s[start] {
i += 1;
}
if s[start] == b'0' {
current = (i - start) as i32;
if prev != -1 {
best = best.max(current + prev);
}
prev = current;
}
}
count_one + best
}
}