There is a street with n * 2 plots, where there are n plots on each side of the street. The plots on each side are numbered from 1 to n. On each plot, a house can be placed.
Return the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street. Since the answer may be very large, return it modulo 109 + 7.
Note that if a house is placed on the ith plot on one side of the street, a house can also be placed on the ith plot on the other side of the street.
Example 1:
Input: n = 1 Output: 4 Explanation: Possible arrangements: 1. All plots are empty. 2. A house is placed on one side of the street. 3. A house is placed on the other side of the street. 4. Two houses are placed, one on each side of the street.
Example 2:
Input: n = 2 Output: 9 Explanation: The 9 possible arrangements are shown in the diagram above.
Constraints:
1 <= n <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a street with a certain number of empty spots where you can either build a house or leave the spot empty, with the rule that no two houses can be placed right next to each other. The brute force approach works by exhaustively looking at every single possible combination of placing and skipping houses for the entire street.
Here's how the algorithm would work step-by-step:
def count_way_to_place_houses(spot_count):
# We generate every possible binary sequence of length equal to spot count
all_combinations = [format(sequence_index, f'0{spot_count}b') for sequence_index in range(1 << spot_count)]
valid_combination_count = 0
# Iterate through every generated combination to check against adjacency rules
for combination_string in all_combinations:
is_valid_sequence = True
for spot_index in range(spot_count - 1):
if combination_string[spot_index] == '1' and combination_string[spot_index + 1] == '1':
is_valid_sequence = False
break
# Only configurations where houses are separated by at least one empty spot count
if is_valid_sequence:
valid_combination_count += 1
# Since houses can be placed independently on both sides of the street, square the result
final_answer = valid_combination_count * valid_combination_count
return final_answerInstead of checking every single possible combination of placing houses on both sides of the street, we can figure out the solution by looking at just one side of the street first. Because the two sides of the street are completely independent, we simply calculate the number of valid ways for one side and then multiply that result by itself to get the grand total.
Here's how the algorithm would work step-by-step:
class Solution:
def countHousePlacements(self, street_length: int) -> int:
# Both sides of the street are independent, so we solve for one side first.
if street_length == 0:
return 1
previous_ways = 1
current_ways = 2
# Build valid ways iteratively using the Fibonacci pattern constraint.
for current_length in range(2, street_length + 1):
next_ways = previous_ways + current_ways
previous_ways = current_ways
current_ways = next_ways
# Multiply combinations from both sides together for the total ways.
total_one_side = current_ways
return (total_one_side * total_one_side) % (10**9 + 7)
| Case | How to Handle |
|---|---|
| Minimum input where n equals 1 plot | The base case logic correctly computes 2 valid placements per side resulting in 4 total ways. |
| Maximum allowed input such as n equals 10000 | An iterative dynamic programming approach avoids recursion limit issues and computes the result in linear time. |
| Large resulting numbers causing integer overflow | Applying the modulo 10 to the power of 9 plus 7 operation at each step prevents integer overflow. |
| Independent side calculation requirement | We compute the valid ways for one side and square the result to account for both sides independently. |
| Zero or negative values for n | The problem constraints specify positive integers starting from 1 so invalid ranges are naturally avoided. |