[LeetCode] 276. 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.
解题思路:
用动态规划(DP),不能有超过连续两根柱子是一个颜色,也就意味着第三根柱子要么根第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色。如果不是同一个颜色,计算可能性的时候就要去掉之前的颜色,也就是k-1种可能性。假设dp[1]是第一根柱子及之前涂色的可能性数量,dp[2]是第二根柱子及之前涂色的可能性数量,则dp[3]=(k-1)*dp[1] + (k-1)*dp[2]。
递推式有了,下面再讨论下base情况,所有柱子中第一根涂色的方式有k中,第二根涂色的方式则是k*k,因为第二根柱子可以和第一根一样。
State:dp[i] // 代表粉刷到第i个桩子总共有多少种刷法
Function: dp[i] = dp[i - 1] * (k - 1) + dp[i - 2] * (k - 1)
Initialize: dp[0] = k, dp[1] = k + dp[0] * (k - 1) = k * k
Return: dp[n]
Java: Time: O(n), Space: O(n)
public class Solution {
public int numWays(int n, int k) {
int dp[] = new int[n + 1];
dp[0] = 0
dp[1] = k;
dp[2] = k * k;
if(n <= 2){
return dp[n];
}
for(int i = 2; i < n; i++){
dp[i] = (k - 1) * (dp[i - 1] + dp[i - 2]);
}
return dp[n];
}
}
Java: Time: O(n), Space: O(1)
public class Solution {
public int numWays(int n, int k) {
int dp[] = {0, k , k*k, 0};
if(n <= 2){
return dp[n];
}
for(int i = 2; i < n; i++){
dp[3] = (k - 1) * (dp[1] + dp[2]);
dp[1] = dp[2];
dp[2] = dp[3];
}
return dp[3];
}
}
[LeetCode] 276. Paint Fence 粉刷篱笆的更多相关文章
- [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 ...
- [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#276] Paint Fence
Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...
- 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:二叉树下的不能相邻,求能 ...
- 276. Paint Fence篱笆涂色
[抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...
- 【LeetCode】276. Paint Fence 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 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]256. Paint House粉刷房子(三色可选)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
随机推荐
- windows 上运行 sslocal 提示 libcrypto(OpenSSL) not found 解决办法
1.下载最新版ss客户端,使用pip安装的并不是最新版,去github下载最新版安装 2.安装openssl客户端 OpenSSL for Windows:https://wiki.openssl.o ...
- Kafaka 总结
Kafka是一个分布式的Streaming处理平台,Kafka可以用于数据库中数据的导入导出,也可以用于实时流的处理,但是Kafka最核心的功能就是作为分布式的消息中间件. Kafka集群是由多个Br ...
- Python2.7 报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
一. 错误原因(网上找的是这样说的,具体的我也不是很了解2.7版本的编码问题): 1.python默认使用ASCII处理字符流. 2.Unicode编码与ASCII编码的不兼容,Python脚本文件是 ...
- Python 3.6 抓取微博m站数据
Python 3.6 抓取微博m站数据 2019.05.01 更新内容 containerid 可以通过 "107603" + user_id 组装得到,无需请求个人信息获取: 优 ...
- 学到了林海峰,武沛齐讲的Day49 django
django 终于等到啦,好东西上场了 blog---- 个体应用文件 model.py 数据库文件 views.py 视图文件 admin.py 后台文件,操纵数据库文件 manage.py --- ...
- java实现ssh登录linux服务器并下发命令
依赖jar包:jsch-0.1.55.jar commons-io-2.5.jar import com.jcraft.jsch.ChannelExec; import com.jcraft.js ...
- php web开发——文件夹的上传和下载
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- chrome调试微信,app中H5网页的方法!
调试微信,app中H5网页大概有如下几个方法: (1).我们可以直接把网页的url放在chrome浏览器中进行调试.(不涉及微信登录) (2).我们可以把网页的url放在微信开发者工具中进行调试. ...
- cyyz: Day 4 网络流整理
Day 4 网络流的理论性知识(算了..我自己都看不下去,还是整理些例题以后复习用吧qaq): 一.PPT(主要内容) 二.搜自度娘 定义: 年,L.R. 福特和 D.R. 富尔克森等人给出了解决 ...
- c语言博客作业03--循环结构
0.展示PTA总分 1.本章学习总结 1.1学习内容总结 循环语句 for语句: for( 表达式1; 表达式2; 表达式3 ) { // 需要执行的语句; } 其执行过程是:表达式 1 首先执行且只 ...