Last active
June 12, 2022 13:18
-
-
Save Rock070/d4587b05f4209867785e0c1a06f77442 to your computer and use it in GitHub Desktop.
Leetcode with JS: 2222. Number of Ways to Select Buildings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numberOfWays = function(s) { | |
/* | |
n0 => numnber of substring of 0 | |
n1 => numnber of substring of 1 | |
n01 => numnber of substring of 01 | |
n10 => numnber of substring of 10 | |
selection must be 101 or 010 | |
*/ | |
let [n0, n1, n01, n10, ans] = [0, 0, 0, 0, 0] | |
for (let i = 0; i < s.length; i++) { | |
if (s[i] === '1') { | |
ans += n10 | |
n01 += n0 | |
n1++ | |
} | |
else { | |
ans += n01 | |
n10 += n1 | |
n0++ | |
} | |
} | |
return ans | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment