File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments