There is a street with n * 2plots , 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 modulo10^9 + 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.
Input: n =1Output: 4Explanation:
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.

Input: n =2Output: 9Explanation: The 9 possible arrangements are shown in the diagram above.
The problem reduces to placing houses on a single row of n plots such that no two houses are adjacent. The number of ways to do this for one row is the (n+2)th Fibonacci number. Since both sides are independent, the answer is the square of this value.
classSolution {
publicintcountHousePlacements(int n) {
int MOD = 1_000_000_007;
long[] f =newlong[n+2];
f[0]= 1; f[1]= 2;
for (int i = 2; i <= n; i++) f[i]= (f[i-1]+ f[i-2]) % MOD;
return (int)(f[n]* f[n]% MOD);
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funcountHousePlacements(n: Int): Int {
val MOD = 1_000_000_007
val f = LongArray(n+2)
f[0] = 1; f[1] = 2for (i in2..n) f[i] = (f[i-1] + f[i-2]) % MOD
return ((f[n] * f[n]) % MOD).toInt()
}
}
1
2
3
4
5
6
7
classSolution:
defcountHousePlacements(self, n: int) -> int:
MOD =10**9+7 f0, f1 =1, 2for _ in range(2, n+1):
f0, f1 = f1, (f0 + f1) % MOD
return (f1 * f1) % MOD
1
2
3
4
5
6
7
8
9
10
11
12
impl Solution {
pubfncount_house_placements(n: i32) -> i32 {
let m =1_000_000_007;
let n = n asusize;
letmut f =vec![0i64; n+2];
f[0] =1; f[1] =2;
for i in2..=n {
f[i] = (f[i-1] + f[i-2]) % m;
}
((f[n] * f[n]) % m) asi32 }
}