当前位置:网站首页>leetcode 统计放置房子的方式数
leetcode 统计放置房子的方式数
2022-06-29 02:29:00 【我很忙2010】
一条街道上共有 n * 2 个 地块 ,街道的两侧各有 n 个地块。每一边的地块都按从 1 到 n 编号。每个地块上都可以放置一所房子。
现要求街道同一侧不能存在两所房子相邻的情况,请你计算并返回放置房屋的方式数目。由于答案可能很大,需要对 109 + 7 取余后再返回。
注意,如果一所房子放置在这条街某一侧上的第 i 个地块,不影响在另一侧的第 i 个地块放置房子。
示例 1:
输入:n = 1 输出:4 解释: 可能的放置方式: 1. 所有地块都不放置房子。 2. 一所房子放在街道的某一侧。 3. 一所房子放在街道的另一侧。 4. 放置两所房子,街道两侧各放置一所。
示例 2:

输入:n = 2 输出:9 解释:如上图所示,共有 9 种可能的放置方式。
提示:
1 <= n <= 104
C++
class Solution {
public:
int countHousePlacements(int n) {
int mod=1000000007;
vector<vector<long>> dp(n,vector<long>(2,0));
dp[0][0]=1;
dp[0][1]=1;
for(int i=1;i<n;i++) {
dp[i][1]=dp[i-1][0]%mod;
dp[i][0]=dp[i-1][1]%mod+dp[i-1][0]%mod;
}
long num=dp[n-1][0]+dp[n-1][1];
return (num%mod)*(num%mod)%mod;
}
};边栏推荐
猜你喜欢

短视频平台常见SQL面试题,你学会了吗?

Chrome browser close update Popup

Use photoshop2022 to create a wonderful gradient effect for pictures
![[sans titre]](/img/36/2f9319e05157ab6a8dd5aa3bef4505.png)
[sans titre]

2022.02.15

Zhongyi technology resumed the review status of the gem IPO, and xuxiaofei no longer acted as a practicing lawyer

Sysbench Pressure Test Oracle (installation and use examples)

Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)

Mipi d-phy -- contents of HS and LP agreements

项目研发,有哪些好用的免费脑图工具软件
随机推荐
Kubernetes: container resource requirements and constraints (constraints)
To apply for a test engineer after years, the resume with high scores should be written like this
组合数据类型之元组小练习
Which brokerage is safer and more convenient to open a stock account for big smart phones?
Wechat applet custom component
跨境资讯站
Understanding and design of high concurrency
Blog publishing test 3
【学习笔记】子集和问题
[redis] key hierarchy
【Redis】List类型
String length
String substitution
字符串方法练习
大三下期末考試
There is a time delay for the click event on the mobile terminal. What is the delay time? How to solve it?
CTFHub-Web-密码口令-默认口令
Is it safe to contact the account manager online to open an account for stock speculation?
Use code binding DataGridView control to display tables in program interface
【Redis】Set类型