Talentd
FleetCode

2320. Count Number of Ways to Place Houses

Medium44.4% acceptance4 companies

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 <= 104

Hints open one at a time

Hint 1
Try coming up with a DP solution for one side of the street.
Hint 2
The DP for one side of the street will bear resemblance to the Fibonacci sequence.
Hint 3
The number of different arrangements on both side of the street is the same.
8:47
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countHousePlacements(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
1
Input
2
Your code is saved on this device.Solution