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 ...
随机推荐
- Daily Scrum6
今天我们小组开会内容分为以下部分: part 1: Anti-spam and anti-abuse module模块总结: part 2: 部分优化代码的展示于交流: part 3:针对用户积分模块 ...
- php中==与===区别
==是不判断二者是否是同一数据类型,而===是更为严格的比较,它不但要求二者值相等,而且还要求它们的数据类型也相同
- .NET设计模式(7):创建型模式专题总结(Creational Pattern)(转)
概述 创建型模式,就是用来创建对象的模式,抽象了实例化的过程.它帮助一个系统独立于如何创建.组合和表示它的那些对象.本文对五种常用创建型模式进行了比较,通过一个游戏开发场景的例子来说该如何使用创建型模 ...
- YTKNetwork
YTKNetwork 是猿题库 iOS 研发团队基于 AFNetworking 封装的 iOS 网络库,其实现了一套 High Level 的 API,提供了更高层次的网络访问抽象. YTKNetwo ...
- awk 统计数据在文件中的出现次数
突然发现awk原来可以统计同一数据在要处理的文件中所出现的次数.原来的时候为了分析数据还自己写程序,哎,无语,当时还以为自己多强,手工分析不过来的东西写程序处理.现在想来实在是年少轻狂.解决问题嘛,不 ...
- Itunes connect State: Developer Action Needed
In-App Purchases have been returned and are highlighted in the table below. Your In-App Purchase has ...
- java基础类:Object类和Math类
1.2.3.4.5.6.7.7.
- ES6中的高阶函数:如同 a => b => c 一样简单
作者:Sequoia McDowell 2016年01月16日 ES6来啦!随着越来越多的代码库和思潮引领者开始在他们的代码中使用ES6,以往被认为是"仅需了解"的ES6特性变成了 ...
- C#&java重学笔记(函数)
C#部分 1.写在函数定义的()中的关键字: a.params关键字:用来满足函数的参数为数组时,而数组的长度不固定的情况.且该关键字只能用来修饰数组型参数.这样一修饰,就达成了类似JavaScri ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...