Link: https://leetcode.com/problems/number-of-ways-to-select-buildings/
Solution:
Intuition
Very cool problem requiring subtle reservoir pattern. The idea is that there are only two possibilities for building selection: 010 and 101
.
The key insight is that when we see a 1, we can form exactly number_of_zeros
01’s…keep the count in zero_ones
. And when we see a 0, we can form exactly number_of_ones
10’s…keep the count in one_zeros
.
The second key insight (building from the first) is that when we see a 1, we can form exactly one_zeros
101’s. And when we see a 0, we can form exactly zero_ones
010’s. Add these to the result.
Implementation
Visual