[Locked] Paint Fence
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.
Note:
n and k are non-negative integers.
分析:
这题看起来能直接用公式输出答案吧...如果觉得公式算起来麻烦,那么对于连续排列串,而且只需要得到可以涂的种类数,故应可用动态规划解决。
代码:
int totalways(int n, int k) {
if(n == || n == || k == )
return k;
int ls = k, ld = k * (k - );
for(int i = ; i < n; i++) {
int temp = ls;
ls = ld;
ld = (temp + ld) * (k - );
}
return ls + ld;
}
[Locked] 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 ...
- 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 ...
- 276. Paint Fence篱笆涂色
[抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...
随机推荐
- Tomcat-java.lang.IllegalArgumentException: Document base F:apps does not exist or is not a readable
启动Tomcat的时候,报错:java.lang.IllegalArgumentException: Document base F:apps does not exist or is not a r ...
- 线程同步(AutoResetEvent与ManualResetEvent)
前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...
- AFNetworking自带的解析图片的方法
首先要导入头文件 #import "UIKit+AFNetworking.h" 方法如下: [personImageView setImageWithURL:[NSURL URLW ...
- Java线程(学习整理)--1--守护线程
1.什么是守护线程? 今天老师讲解我才知道有守护线程这回事!原来守护线程经常存在于我们的身边,比如:一个免费的网页游戏,里面都会或多或少有些插入性的广告!! 一般情况下,我们不会去点击这些广告的,但是 ...
- SGU 154.Factorial
时间限制:0.25s 空间限制:4M 题意 你的任务是找到最小自然数 N, 使N!在十进制下包含 Q个零. 众所周知 N! = 1*2*...*N. 例如, 5! = 120, 120 结尾包含1个零 ...
- Web::Scraper 页面提取分析
一组用来提取HTML文档中元素内容的工具集,它能够理解HTML和CSS选择器以及XPath表达式. 语法 use URI; use Web::Scraper; # First, create your ...
- printf 格式化最常用用法
printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...
- 从windows到Linux-ubuntu新手
版本选择: 经多次实验,Ubuntu个人认为长期支持(LTS)版才值得装. VMware9中测试:Ubuntu10.04开机内存170M,Ubuntu12.04开机内存340M. 个人感觉Ubuntu ...
- hdu 3308
终于A了,我好想砍人,虽然这是一道基础的区间合并.但是这错误我也是醉了. 错误我表在注释里. 题目意思不多说,sha崽题目出的很简洁. #include <iostream>#includ ...
- 如何使一个input文本框随其中内容而变化长度。
第一:<input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(th ...