Leetcode 2320: Count Number of Ways to Place Houses

grid47
grid47
Exploring patterns and algorithms
Mar 20, 2024 6 min read

Given a street with two rows of plots, each containing n plots, find the number of ways houses can be placed such that no two houses are adjacent on the same side of the street. Return the result modulo 10^9 + 7.
Problem
Approach
Steps
Complexity
Input: The input consists of an integer n, representing the number of plots on each side of the street.
Example: n = 1
Constraints:
• 1 <= n <= 10^4
Output: Return the number of valid ways to place houses modulo 10^9 + 7.
Example: For n = 1, the output is 4.
Constraints:
• The result should be returned modulo 10^9 + 7.
Goal: To calculate the number of ways to place houses, we need to ensure that houses are not adjacent on the same side of the street. The result depends on valid placement configurations for both sides.
Steps:
• 1. Use dynamic programming to calculate the number of valid configurations for each side of the street.
• 2. Multiply the valid configurations for each side and return the result modulo 10^9 + 7.
Goal: The value of n is constrained between 1 and 10^4, and the result must be computed modulo 10^9 + 7.
Steps:
• 1 <= n <= 10^4
Assumptions:
• The street has exactly two sides, and both sides have n plots.
Input: n = 1
Explanation: For n = 1, the possible configurations are: no house, one house on the first side, one house on the second side, and one house on both sides. Hence, the total number of valid configurations is 4.

Input: n = 2
Explanation: For n = 2, the number of valid configurations is calculated by considering the valid placement of houses on each side and multiplying them together, resulting in 9 possible configurations.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus