Paint Fence
There 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.
Notice
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
这种给定一个规则,计算有多少种结果的题目一般都是动态规划,因为我们可以从这个规则中得到递推式。根据题意,不能有超过连续两根柱子是一个颜色,也就意味着第三根柱子要么跟第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色。如果不是同一个颜色,计算可能性的时候就要去掉之前的颜色,也就是k-1种可能性。假设dp[1]是第一根柱子及之前涂色的可能性数量,dp[2]是第二根柱子及之前涂色的可能性数量,则dp[3]=(k-1)*dp[1] + (k-1)*dp[2]。
另一种理解是第三根柱子只有两种选择:跟第二根一个颜色dp[1]*(k-1),跟第二根不同颜色dp[2]*(k-1), 这两种情况是不互相包含的,是独立的,
public int numWays(int n, int k) {
// 当n=0时返回0
int dp[] = {, k , k*k, };
if(n <= ){
return dp[n];
}
for(int i = ; i <= n; i++){
// 递推式:第三根柱子要么根第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色
dp[] = (k - ) * (dp[] + dp[]);
dp[] = dp[];
dp[] = dp[];
}
return dp[];
}
Reference:
https://segmentfault.com/a/1190000003790650
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 ...
- 276. Paint Fence篱笆涂色
[抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...
随机推荐
- git学习心得(1)
这次是第一次用git这个平台,还非常不熟练,尤其是在上传代码的步骤上,有好几次都出了问题,多次询问了同学才将代码上传到平台上,git是一个分布式版本控制系统,相对于集中式的来说具有很多优点,但是中文资 ...
- 从编译DotNetOpenAuth中学到的程序集强签名知识
1. 背景 最近在研究DotNetOpenAuth——OAuth的一个.NET开源实现,官方网站:http://dotnetopenauth.net/ . 从GitHub签出DotNetOpenAut ...
- 通过第三方软件打开sqlite
1.SQLite Expert 使用之前,可以先下载SQLite Expert,方便查看.db3数据库 下载链接是http://www.sqliteexpert.com/ 可以谷歌找到licen ...
- MongoDB入门 和nodejs操作
简介 MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshel ...
- 【IneliJ 】使用IneliJ IDEA 2016将Java Web项目导出为War包
本文记录使用IDEA导出war包的过程以及碰到问题的解决办法 虽说现在改用IDEA进行开发了,但还是用eclipse打war包 ….囧 这样下去不是办法... 于是今天就试着使用IDEA进行打包. 项 ...
- Java之utf8中文编码转换
import org.apache.commons.codec.binary.Hex; import org.junit.Test; /** * HBASE中文转换 */ public class C ...
- poj3468 A Simple Problem with Integers(线段树/树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- TCP 协议连接与关闭的握手
原文链接 http://blog.csdn.net/oney139/article/details/8103223 TCP头部: 其中 ACK SYN 序号 这三个部分在以下会用到,它们 ...
- Java之Set的使用场景
2.Set使用场景 API介绍: java.util.Set接口和java.util.List接口一样,同样继承自Collection接口, 它与Collection接口中的方法基本一致,并没有对Co ...
- 【刷题】LOJ 6011 「网络流 24 题」运输问题
题目描述 W 公司有 \(m\) 个仓库和 \(n\) 个零售商店.第 \(i\) 个仓库有 \(a_i\) 个单位的货物:第 \(j\) 个零售商店需要 \(b_j\) 个单位的货物.货物供需平衡, ...