Skip to content

Commit c23c35d

Browse files
authored
Create 2380-time-needed-to-rearrange-a-binary-string.js
1 parent 6b72ba5 commit c23c35d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var secondsToRemoveOccurrences = function(s) {
6+
const n = s.length
7+
let zeros = 0, seconds = 0;
8+
for (let i = 0; i < n; ++i) {
9+
zeros += s.charAt(i) == '0' ? 1 : 0;
10+
if (s.charAt(i) == '1' && zeros > 0)
11+
seconds = Math.max(seconds + 1, zeros);
12+
}
13+
return seconds;
14+
};

0 commit comments

Comments
 (0)