Input: s ="01"Output: 1Explanation:
Because there is no block of `'1'`s surrounded by `'0'`s, no valid trade ispossible. The maximum number of active sections is1.
Input: s ="0100"Output: 4Explanation:
* String `"0100"`-> Augmented to `"101001"`.* Choose `"0100"`, convert `"10 _**1**_ 001"`->`"1 _**0000**_ 1"`->`"1 _**1111**_ 1"`.* The final string without augmentation is`"1111"`. The maximum number of active sections is4.
Input: s ="1000100"Output: 7Explanation:
* String `"1000100"`-> Augmented to `"110001001"`.* Choose `"000100"`, convert `"11000 _**1**_ 001"`->`"11 _**000000**_ 1"`->`"11 _**111111**_ 1"`.* The final string without augmentation is`"1111111"`. The maximum number of active sections is7.
Input: s ="01010"Output: 4Explanation:
* String `"01010"`-> Augmented to `"1010101"`.* Choose `"010"`, convert `"10 _**1**_ 0101"`->`"1 _**000**_ 101"`->`"1 _**111**_ 101"`.* The final string without augmentation is`"11110"`. The maximum number of active sections is4.
We want to maximize the number of active sections by performing at most one trade, which consists of removing a block of ‘1’s (surrounded by ‘0’s) and then flipping a block of ‘0’s (surrounded by ‘1’s). By simulating all possible valid trades, we can find the optimal result.
classSolution {
public:int maximizeActiveSections(string s) {
string t ="1"+ s +"1";
int n = t.size();
vector<pair<int,int>> ones, zeros;
int i =0;
while (i < n) {
if (t[i] =='1'&& t[i-1] =='0') {
int j = i;
while (j < n && t[j] =='1') j++;
if (t[j] =='0') ones.push_back({i, j-1});
i = j;
} elseif (t[i] =='0'&& t[i-1] =='1') {
int j = i;
while (j < n && t[j] =='0') j++;
if (t[j] =='1') zeros.push_back({i, j-1});
i = j;
} else {
i++;
}
}
int ans = count(s.begin(), s.end(), '1');
for (auto& o : ones) {
for (auto& z : zeros) {
string tmp = t;
for (int k = o.first; k <= o.second; ++k) tmp[k] ='0';
for (int k = z.first; k <= z.second; ++k) tmp[k] ='1';
int cnt =0;
for (int k =1; k < n-1; ++k) if (tmp[k] =='1') cnt++;
ans = max(ans, cnt);
}
}
return ans;
}
};
classSolution {
publicintmaximizeActiveSections(String s) {
String t ="1"+ s +"1";
int n = t.length();
List<int[]> ones =new ArrayList<>(), zeros =new ArrayList<>();
int i = 0;
while (i < n) {
if (t.charAt(i) =='1'&& t.charAt(i-1) =='0') {
int j = i;
while (j < n && t.charAt(j) =='1') j++;
if (t.charAt(j) =='0') ones.add(newint[]{i, j-1});
i = j;
} elseif (t.charAt(i) =='0'&& t.charAt(i-1) =='1') {
int j = i;
while (j < n && t.charAt(j) =='0') j++;
if (t.charAt(j) =='1') zeros.add(newint[]{i, j-1});
i = j;
} else {
i++;
}
}
int ans = 0;
for (char c : s.toCharArray()) if (c =='1') ans++;
for (int[] o : ones) {
for (int[] z : zeros) {
char[] tmp = t.toCharArray();
for (int k = o[0]; k <= o[1]; k++) tmp[k]='0';
for (int k = z[0]; k <= z[1]; k++) tmp[k]='1';
int cnt = 0;
for (int k = 1; k < n-1; k++) if (tmp[k]=='1') cnt++;
ans = Math.max(ans, cnt);
}
}
return ans;
}
}
classSolution {
funmaximizeActiveSections(s: String): Int {
val t = "1" + s + "1"val n = t.length
val ones = mutableListOf<Pair<Int, Int>>()
val zeros = mutableListOf<Pair<Int, Int>>()
var i = 0while (i < n) {
if (t[i] =='1'&& t[i-1] =='0') {
var j = i
while (j < n && t[j] =='1') j++if (t[j] =='0') ones.add(i to j-1)
i = j
} elseif (t[i] =='0'&& t[i-1] =='1') {
var j = i
while (j < n && t[j] =='0') j++if (t[j] =='1') zeros.add(i to j-1)
i = j
} else {
i++ }
}
var ans = s.count { it=='1' }
for (o in ones) {
for (z in zeros) {
val tmp = t.toCharArray()
for (k in o.first..o.second) tmp[k] = '0'for (k in z.first..z.second) tmp[k] = '1'val cnt = (1 until n-1).count { tmp[it] =='1' }
ans = maxOf(ans, cnt)
}
}
return ans
}
}
classSolution:
defmaximizeActiveSections(self, s: str) -> int:
t ='1'+ s +'1' n = len(t)
ones, zeros = [], []
i =0while i < n:
if t[i] =='1'and t[i-1] =='0':
j = i
while j < n and t[j] =='1':
j +=1if t[j] =='0':
ones.append((i, j-1))
i = j
elif t[i] =='0'and t[i-1] =='1':
j = i
while j < n and t[j] =='0':
j +=1if t[j] =='1':
zeros.append((i, j-1))
i = j
else:
i +=1 ans = s.count('1')
for o in ones:
for z in zeros:
tmp = list(t)
for k in range(o[0], o[1]+1):
tmp[k] ='0'for k in range(z[0], z[1]+1):
tmp[k] ='1' cnt = sum(1for k in range(1, n-1) if tmp[k] =='1')
ans = max(ans, cnt)
return ans
impl Solution {
pubfnmaximize_active_sections(s: String) -> i32 {
let t =format!("1{}1", s);
let n = t.len();
let t = t.chars().collect::<Vec<_>>();
letmut ones =vec![];
letmut zeros =vec![];
letmut i =0;
while i < n {
if t[i] =='1'&& t[i-1] =='0' {
letmut j = i;
while j < n && t[j] =='1' { j +=1; }
if t[j] =='0' { ones.push((i, j-1)); }
i = j;
} elseif t[i] =='0'&& t[i-1] =='1' {
letmut j = i;
while j < n && t[j] =='0' { j +=1; }
if t[j] =='1' { zeros.push((i, j-1)); }
i = j;
} else {
i +=1;
}
}
letmut ans = s.chars().filter(|&c| c =='1').count() asi32;
for&(o1, o2) in&ones {
for&(z1, z2) in&zeros {
letmut tmp = t.clone();
for k in o1..=o2 { tmp[k] ='0'; }
for k in z1..=z2 { tmp[k] ='1'; }
let cnt = (1..n-1).filter(|&k| tmp[k] =='1').count() asi32;
ans = ans.max(cnt);
}
}
ans
}
}