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 ...
随机推荐
- ThinkPHP可以支持直接使用字符串作为查询条件
ThinkPHP可以支持直接使用字符串作为查询条件,但是大多数情况推荐使用数组或者对象来作为查询条件,因为会更加安全. 大理石平台哪家好 一.使用字符串作为查询条件 这是最传统的方式,但是安全性不高, ...
- ThinkPHP 读取数据
在ThinkPHP中读取数据的方式很多,通常分为读取数据.读取数据集和读取字段值. 步进电机和伺服电机 数据查询方法支持的连贯操作方法有: 连贯操作 作用 支持的参数类型 where 用于查询或者更新 ...
- Cannot find module '@babel/plugin-proposal-class-properties'
cnpm install --save-dev @babel/plugin-proposal-class-properties
- Mac 下搭建vue开发环境
tips:一定要有翻墙工具如lanter,另外要保证网速OK. 1. 首先需要安装homebrew liukingdeMBP:~ liuking$ /usr/bin/ruby -e "$(c ...
- 实用的 JavaScript 调试小技巧
‘debugger;’ 除了console.log,debugger就是另一个我很喜欢的快速调试的工具,将debugger加入代码之后,Chrome会自动在插入它的地方停止,很像C或者Java里面打断 ...
- 关于Collection接口和Map
Iterable才是Collection的父接口.不是Iterator. Map,SortedMap属于接口类型,不可以new的方式创建对象. HashMap基于哈希表实现Map接口的类,并允许nul ...
- linux最基础最常用的命令快速手记 — 让手指跟上思考的速度(三)
这一篇作为姐妹篇的第三篇,废话不多说,我觉得这个比mysql的还要重要,为什么,一旦你摊上linux 敲键盘输入命令简直是要飞的速度,不断的卡壳查命令,效率太低了,而且非常严重的影响思绪,思绪! 某些 ...
- php 随意参数方法的使用
1, 用到的PHP函数: func_get_arg() / func_get_args()/ func_num_args 2, func_get_arg(index) :根据索引取得参数具体值 ...
- json-server 和mock.js生成大量json数据
JSON-server和mock.jsmock文件夹下 db.json db.jsjson-sever使用 安装:npm install json-server -g/mock 目录下执行json-s ...
- final,finally和finalize之间的区别
(1)final用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承.内部类要访问局部变量,局部变量必须定义成final类型,比如一段代码 (2)finally是异常处理语句结构的一部 ...