[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 ...
随机推荐
- oracle获取时间段之间的所有日期
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10', 'yyyy-MM'), ROWNUM - 1), 'yyyy-MM') as monthlist FROM D ...
- KClass与函数引用详解
继续学习Kotlin反射相关的东东. KClass: 在上一次是通过类来获取它的KClass对象: 那如果是一个对象呢?与这个对象对应的类的KClass对象又是如何获取的呢?像Java也是一样有相关机 ...
- kafka题目
1. Kafka的用途有哪些?使用场景如何?2. Kafka中的ISR.AR又代表什么?ISR的伸缩又指什么3. Kafka中的HW.LEO.LSO.LW等分别代表什么?4. Kafka中是怎么体现消 ...
- .netcore发布时指定服务器的系统类型
asp.net core 开发完成后发布,在IIS上面访问,直接报错 系统是windows2008 Application startup exception: System.DllNotFound ...
- apscheduler 执行报错No handlers could be found for logger "apscheduler.executors.default
执行报错如下: No handlers could be found for logger "apscheduler.executors.default 解决: 加入日志,查看具体报错,载根 ...
- 线性回归和Ridge回归
网址:https://www.cnblogs.com/pinard/p/6023000.html 线性回归和交叉验证 import matplotlib.pyplot as plt import nu ...
- HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5
思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...
- 查看.NET应用程序中的异常(上)
内存转储是查明托管.NET应用程序中异常的原因的一种极好的方法,特别是在生产应用程序中发生异常时.当您在无法使用Visual Studio的应用程序中跟踪异常时,cdb和sos.dll的使用技术就变成 ...
- 【批处理】if命令,注释方式
If 命令 if 表示将判断是否符合规定的条件,从而决定执行不同的命令. 有三种格式:1.if "参数" == "字符串" 待执行的命令参数如果等于指定的字符串 ...
- video.js学习笔记
video.js学习笔记获取用户观看时长