LintCode_514 Paint Fence
题目
here is a fence with n posts, each post can be painted with one of the k colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.
注意事项
n and k are non-negative integers.
Given n=3, k=2 return 6
post 1, post 2, post 3思路
way1 0 0 1
way2 0 1 0
way3 0 1 1
way4 1 0 0
way5 1 0 1
way6 1 1 0
动态规划问题
当我前i个posts有多少种可能性是 如果第i-1个颜色和第i个颜色不同时 s[i] = (k-1)*s[i-1];
如果第i-1个颜色和第i个颜色相同时因为不能有超过2个颜色相同,则和第i-2个颜色一定不同 s[i] = s[i-2]*(k-1);
s[i] = s[i-1]*(k-1) + s[i-2]*(k-1)
C++代码
int numWays(int n, int k) {
// Write your code here
if(n == ) return ;
if(n == ) return k;
if(n == ) return k*k;
int i;
int a[n];
a[] = k;
a[] = k*k;
for(i = ; i < n; ++i)
{
a[i] = a[i-]*(k-) + a[i-]*(k-);
}
return a[i-];
}
LintCode_514 Paint Fence的更多相关文章
- [LintCode] Paint Fence 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...
- [Locked] Paint Fence
Paint Fence There is a fence with n posts, each post can be painted with one of the k colors. You ha ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- [LeetCode] Paint Fence 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- LeetCode Paint Fence
原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...
- 276. Paint Fence
题目: There is a fence with n posts, each post can be painted with one of the k colors. You have to pa ...
- [LeetCode#276] Paint Fence
Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...
- [Swift]LeetCode276. 粉刷栅栏 $ Paint Fence
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- Paint Fence
There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...
随机推荐
- 线性dp——cf1032
升维来保存第i位按j是否可行,然后枚举i-1个的状态,用5*5n就可以完成递推 /* dp[i][j]==0表示第i步按j不可行 */ #include<bits/stdc++.h> us ...
- 安卓手机端微信网页浏览记录清理debugx5.qq.com
最近我们环境从复)星(云切换到阿里云.早上地铁路上就有小伙伴@,一阵搜索.找的如下的方法. 记录一下: 目前只支持安卓手机的微信内置浏览器清理. 由腾讯提供的网址http://debugx5.qq.c ...
- 从三层架构到Spring框架
首先是软件的应用分层架构 标准三层架构: 1:数据访问层:实现了数据的持久化 2:业务逻辑层:对逻辑的实现及处理,实际上不可能在表示层对数据不做任何处理,但是尽可能的将逻辑分为一层 3:表示层:数据的 ...
- 运行第一个python程序,python 变量,常量,注释
一.运行第一个python程序: print('Hello,world') 保存为.py文件 在cmd窗口: python3x:python py文件路径 回车 python2x:python p ...
- <数据库>MySQL补充( 查询)
show create table 表名 \G;(查看创建的属性) alter table 表名 auto_increment=xx;(修改自增起始值) set session auto_increm ...
- C++面向对象高级编程(下)第一周-Geekband
勿在浮沙筑高台 革命尚未成功,同志仍需努力 <h1> Conversion Function</h1> class Fraction { public: Fraction(in ...
- HBase功能组件
- linux使用wget
wget is a Linux command-line utility for retrieving files from the web, via HTTP, HTTPS and FTP prot ...
- EasyUI Tree与Datagrid联动
效果图 这是一个简单的solr检索的例子 输入关键词,显示树 选择一个节点,得到该节点下文档信息 代码: JSP: 重点是标红的URL传递 <body> <d ...
- 常见的5个runtime exception
NullPointException(空指针异常),ArrIndexOutOfBoundsException(数组越界异常),ClassCastException(类型转换异常),ClassNotFo ...