Number of Ways to Select Buildings - Complete Solution Guide
Number of Ways to Select Buildings is LeetCode problem 2222, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed binary string s which represents the types of buildings along a street where: s[i] = '0' denotes that the i th building is an office and s[i] = '1' denotes that the i th building is a restaurant. As a city official, you would like to select 3 buildings for random inspection. However, to ensure variety, no two consecutive buildings out of the selected buildings can be of the same type. For example, given s = "0 0 1 1 0 1 " , we cannot select the 1 st , 3 rd , and 5 th bu
Detailed Explanation
The problem asks us to find the number of ways to select three buildings from a street such that no two consecutive selected buildings are of the same type. The street is represented by a binary string 's' where '0' represents an office and '1' represents a restaurant. We need to count the number of valid triplets of building indices that alternate between offices and restaurants (or vice-versa). For example, "010" and "101" are valid selections, but "001" or "110" are not.
Solution Approach
The provided solution iterates through the input string 's' once. For each character in 's', it considers that character as the middle element of a potential valid sequence ('010' or '101'). If the character is '0', it calculates the number of '1's to its left and the number of '1's to its right. The product of these two numbers represents the number of '101' sequences with that '0' as the middle element. Similarly, if the character is '1', it calculates the number of '0's to its left and the number of '0's to its right, and their product gives the number of '010' sequences with that '1' as the middle element. The sum of these products for all characters in 's' gives the total number of valid ways to select the buildings.
Step-by-Step Algorithm
- Step 1: Calculate the total number of '0's and '1's in the input string 's'.
- Step 2: Initialize variables to track the number of '0's and '1's encountered so far while iterating through the string.
- Step 3: Iterate through the string 's'.
- Step 4: In each iteration, check if the current character is '0' or '1'.
- Step 5: If the character is '0', calculate the number of '1's to the left (ones_so_far) and to the right (total_ones - ones_so_far). Add their product to the answer.
- Step 6: If the character is '1', calculate the number of '0's to the left (zeros_so_far) and to the right (total_zeros - zeros_so_far). Add their product to the answer.
- Step 7: Update the count of '0's or '1's encountered so far, depending on the character.
- Step 8: After iterating through the entire string, return the calculated answer.
Key Insights
- Insight 1: We need to find triplets of the form '010' or '101'. The solution focuses on iterating through the string and considering each character as the middle element of a potential triplet.
- Insight 2: Prefix sums (or counting occurrences so far) are crucial to efficiently determine the number of '0's and '1's to the left and right of each character.
- Insight 3: Avoid nested loops. By pre-calculating the total number of zeros and ones and using prefix sums, the solution achieves linear time complexity.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Dynamic Programming, Prefix Sum.
Companies
Asked at: Dream11.