2320. Count Number of Ways to Place Houses
Question
There is a street with
n * 2
plots, where there aren
plots on each side of the street. The plots on each side are numbered from1
ton
. 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
10<sup>9</sup><span> </span>+ 7
.Note that if a house is placed on the
i<sup>th</sup>
plot on one side of the street, a house can also be placed on thei<sup>th</sup>
plot on the other side of the street.
Solution
计算单侧河道可以放置的种类。
一开始用的是DFS搜素来计算可以放置的种类。
计算后发现是以(2,3)开始的斐波那契数列。
因此直接计算n位置的斐波那契数列值即可。
然后两边可以匹配的种类就是其平方。
记忆化搜索
在计算斐波那契数列时,由于要多次重复计算前面的位置,因此可以采用记忆化搜索记录对应的斐波那契额值。
Code
1 | class Solution { |
2320. Count Number of Ways to Place Houses
https://xuanhe95.github.io/2022/06/26/2320-Count-Number-of-Ways-to-Place-Houses/