Skip to content

Commit 20c3549

Browse files
authored
Create 3499-maximize-active-section-with-trade-i.js
1 parent d1c49db commit 20c3549

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var maxActiveSectionsAfterTrade = function (s) {
6+
const n = s.length
7+
const c1 = Array.from(s).filter((char) => char === '1').length
8+
9+
let ans = 0
10+
let i = 0
11+
while (i < n) {
12+
if (s[i] === '1') {
13+
const start = i
14+
while (i < n && s[i] === '1') {
15+
i++
16+
}
17+
const end = i - 1
18+
if (start > 0 && end < n - 1) {
19+
let leftZeros = 0
20+
let j = start - 1
21+
while (j >= 0 && s[j] === '0') {
22+
leftZeros++
23+
j--
24+
}
25+
let rightZeros = 0
26+
j = end + 1
27+
while (j < n && s[j] === '0') {
28+
rightZeros++
29+
j++
30+
}
31+
const more = leftZeros + rightZeros
32+
ans = Math.max(ans, more)
33+
}
34+
} else {
35+
i++
36+
}
37+
}
38+
return c1 + ans
39+
}

0 commit comments

Comments
 (0)