Leetcode Problem 2222. Number of Ways to Select Buildings

2222. Number of Ways to Select Buildings

Leetcode Solutions

Prefix Sum Approach

  1. Initialize two variables to count the total number of '0's and '1's in the string.
  2. Initialize two variables to keep track of the number of '0's and '1's encountered so far as we iterate through the string.
  3. Iterate through the string from left to right.
  4. For each character, if it is '0', calculate the product of the count of '1's before this '0' and the count of '1's after this '0'. Add this product to the answer.
  5. If the character is '1', calculate the product of the count of '0's before this '1' and the count of '0's after this '1'. Add this product to the answer.
  6. Update the count of '0's or '1's encountered so far.
  7. Return the total count as the answer.
UML Thumbnail

Dynamic Programming with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...