Skip to content

Instantly share code, notes, and snippets.

@Rock070
Last active June 12, 2022 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rock070/d4587b05f4209867785e0c1a06f77442 to your computer and use it in GitHub Desktop.
Save Rock070/d4587b05f4209867785e0c1a06f77442 to your computer and use it in GitHub Desktop.
Leetcode with JS: 2222. Number of Ways to Select Buildings
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