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.
链接: http://leetcode.com/problems/paint-fence/
题解:
又是数学题,给篱笆涂色,相邻最多两个post可以同色。第一思路就是Dynamic Programming了。代码大都参考了Discuss的Jenny_Shaw的。要注意的就是每次计算, 当前的结果应该等于sameColor和differentColor的和,而differentColor只能在k - 1种color里选,等于之前结果 * (k - 1), sameColor等于之前的differentColor,之后进行下一次计算。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int numWays(int n, int k) {
if(n <= 0 || k <= 0) {
return 0;
}
if(n == 1) {
return k;
}
int sameColor = k;
int differentColor = k * (k - 1); for(int i = 2; i < n; i++) {
int tmp = differentColor;
differentColor = (sameColor + differentColor) * (k - 1);
sameColor = tmp;
} return sameColor + differentColor;
}
}
二刷:
依然是使用dp。题目给定最多两个fence可以用一种颜色喷漆。下面我们来仔细分析一下。
- 首先我们判断边界的条件,n <=0, k <= 0, n == 1
- 我们初始化两个变量,sameColorLastTwo和diffColorLastTwo, 假如位于0和1位置的两个fence用一种颜色喷的话,那么我们可以设定sameColorLastTwo = k, 假如它们用两种颜色喷的话,那么我们可以设定diffColorLastTwo = k * (k - 1)
- 接下来我们从i到n开始遍历,我们设定diffColorLastTwo + sameColorLastTwo等于到第i位之前,我们一种有多少种喷漆方法
- 先建立一个tmp保存当前的diffColorLastTwo,也就是i-1位与i-2位使用不同颜色
- 假如我们我们第i位,不使用与第i-1位相同的颜色,那么我们更新diffColorLastTwo = (diffColorLastTwo + sameColorLastTwo) * (k - 1)
- 假如我们第i位使用和第i - 1位相同的颜色,那么第i位的sameColorLastTwo = 第i - 1位的diffColorLastTwo
- 遍历完第n-1位后返回结果sameColorLastTwo + diffColorLastTwo
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int numWays(int n, int k) {
if (n <= 0 || k <= 0) {
return 0;
}
if (n == 1) {
return k;
}
int sameColorLastTwo = k;
int diffColorLastTwo = k * (k - 1);
for (int i = 2; i < n; i++) {
int tmp = diffColorLastTwo;
diffColorLastTwo = (sameColorLastTwo + diffColorLastTwo) * (k - 1);
sameColorLastTwo = tmp;
}
return sameColorLastTwo + diffColorLastTwo;
}
}
三刷:
换了一点点写法,看起来更简洁,不过最后多做了两次计算操作。
Java:
public class Solution {
public int numWays(int n, int k) {
if (n <= 0 || k <= 0) return 0;
if (n == 1) return k;
int res = 0;
int sameColorLastTwo = k, diffColorLastTwo = k * (k - 1);
for (int i = 2; i <= n; i++) {
res = sameColorLastTwo + diffColorLastTwo;
sameColorLastTwo = diffColorLastTwo;
diffColorLastTwo = res * (k - 1);
}
return res;
}
}
Reference:
https://leetcode.com/discuss/56173/o-n-time-java-solution-o-1-space
https://leetcode.com/discuss/58451/java-dp-solution
https://leetcode.com/discuss/58879/python-solution-with-explanation
https://leetcode.com/discuss/56245/lucas-formula-maybe-o-1-and-3-4-liners
https://leetcode.com/discuss/62587/7-lines-no-special-case-code-o-n-o-1
276. Paint Fence的更多相关文章
- 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#276] Paint Fence
Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...
- 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 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- 【LeetCode】276. Paint Fence 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [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] 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 ...
随机推荐
- rapid js framework
allcountjs.com http://mean.io/ https://www.meteor.com/ http://sailsjs.org/#!/ nodejs 博客 http://hexo. ...
- Understanding Convolutions【转】
Understanding Convolutions In a previous post, we built up an understanding of convolutional neural ...
- 学习Linux第四天
---恢复内容开始--- 1.常用的命令: reset 清屏 leave +hhmm 建立离开提醒 sudo apt-get yum 安装yum程序 sudo su 切换root身份 see test ...
- JS--中的 Cookie 与存储
Cookie 主要是在客户端进行一些简单的数据存储等,使用来提供本地化存储的脚本功能.Cookie 的处理环境本身是需要在服务器下进行的,但是现在的大部分浏览器都已经支持Cookie本地化的存储于处理 ...
- JS 学习笔记--13---原型
练习中使用的是IE11,如果有错误之处,还请各位朋友多多指教.本文关于原型难以描述,故多用代码展示 原型是JS中一个很重要的概念,也是JS中一个难点,语言上难以描述,原型对象的属性和方法叫做原型属性和 ...
- SQL Server表分区案例
--学习创建表分区脚本/*SQL SERVER 2005中以上版本,终于引入了表分区,就是说,当一个表里的数据很多时,可以将其分拆到多个的表里,大大提高了性能.下面举例子说明之*/ --------- ...
- lua与 object-C 通信
IOS中如何调用LUA,以及LUA如何调用IOS中的功能 下面将讲解一下如何在iOS里调用Lua函数,以及Lua函数如何调用iOS本地函数. 转载请注明出处.原文出处 http://www.cnblo ...
- 使用NPOI和线程池快速加载EXCEL数据
private void FilterData() { List<Task> tasks = new List<Task>(); IWorkbook workbook = Cs ...
- winform 记录全局异常捕获
这篇文章主要是备用 记录winform程序捕获全局异常. /// <summary> /// 应用程序的主入口点. /// </summary> public static A ...
- pragma伪指令
pragma伪指令 通过pragma伪指令告诉编译器如何对待特定的函数.对象或代码段.TMS320C28x C/C++编译器支持如下形式的pragma伪指令: CODE_SECTION(func,“s ...