Code 靈感來自於 House Robber III 用一個 size = 2 的 vector 去紀錄不同狀況的 return 值,這裡也是一樣,使用 house & space 去紀錄當前格子有放房子和沒有放的方法數。 class Solution { public: int countHousePlacements(int n) { long long mod = 1e9 + 7; long long space = 1; long long house = 1; long long total = space + house; for(int i = 2; i <= n; i++) { house = space; space = total; total = (space + house) % mod; } return (total * total) % mod; } }; Link Count Number of Ways to Place Houses