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 ...
随机推荐
- 基于.net mvc的校友录(五、web.config对的配置以及filter实现的权限控制)
web.config配置文件 此文件是整个系统的配置中心,它告诉iis服务器本网站需要哪些运行时环境,需要哪些环境,将要进行哪些操作,开发人员也会将一个常量性的数据放在此配置中,以备系统全局调用.此文 ...
- LeetCode-Implement strStr()-KMP
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 寻找“水王”--c++
一.题目与设计思路 三人行设计了一个灌水论坛.信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子.坊间风闻该“水王”发帖数目超过了帖子数目的一 ...
- js—对象
一.创建对象 var car = new Object(); var car = {}; 二.属性和方法 var car = { color : "red", run : func ...
- android开发,关于android app实现静默安装自己(系统签名)
产品需求,木有办法.android系统是跟厂商定制的,保证系统开机就运行我们的app,并且实现自己静默安装,完全自动化,无需人工操作. 网上有很多办法, 1.要么要通过android 源码拿到密钥文件 ...
- Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...
- selenium--下拉列表选择
html 代码: <select name="ColumnId" id="columnOrd"> <option value="2& ...
- eclipse编辑jsp文件和javascript代码很卡解决办法
1.Window -> Preference -> General -> Editors -> Text Editors -> HyperLinking -> 在用 ...
- NYOJ-32 组合数 AC 分类: NYOJ 2013-12-30 07:42 189人阅读 评论(0) 收藏
#include<stdio.h> int num[100]; int pnum(int n,int v); int mv=0; int main(){ int n,v; scanf(&q ...
- [开发]Win7环境下Eclipse连接Hadoop2.2.0
准备: 确保hadoop2.2.0集群正常运行 1.eclipse中建立mven工程,并编辑pom文件如下 <dependencies> <dependency> <gr ...