Count Number of Ways to Place Houses LeetCode solution in Python
1class Solution:2def countHousePlacements(self, n: int) -> int:34@lru_cache(None)5def rec(i, k):67# i is the index of the house8# k is the state of last house, 1 if there was a house on the last index else 0910if i >= n:11return 11213elif k == 0:14return rec(i + 1, 1) + rec(i + 1, 0)1516else:17return rec(i + 1, 0)1819# l1 are the combinations possible in lane 1, the final answer will be the square20# of of l1 as for every combination of l1 there will be "l1" combinations in lane2.2122l1 = rec(1, 0) + rec(1, 1)2324mod = 10**9 + 725return pow(l1, 2, mod) # use this when there is mod involved along with power