题目:

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可以用一种颜色喷漆。下面我们来仔细分析一下。

  1. 首先我们判断边界的条件,n <=0,  k <= 0, n == 1
  2. 我们初始化两个变量,sameColorLastTwo和diffColorLastTwo, 假如位于0和1位置的两个fence用一种颜色喷的话,那么我们可以设定sameColorLastTwo = k,  假如它们用两种颜色喷的话,那么我们可以设定diffColorLastTwo = k * (k - 1)
  3. 接下来我们从i到n开始遍历,我们设定diffColorLastTwo + sameColorLastTwo等于到第i位之前,我们一种有多少种喷漆方法
    1. 先建立一个tmp保存当前的diffColorLastTwo,也就是i-1位与i-2位使用不同颜色
    2. 假如我们我们第i位,不使用与第i-1位相同的颜色,那么我们更新diffColorLastTwo = (diffColorLastTwo + sameColorLastTwo) * (k - 1)
    3. 假如我们第i位使用和第i - 1位相同的颜色,那么第i位的sameColorLastTwo = 第i - 1位的diffColorLastTwo
  4. 遍历完第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的更多相关文章

  1. 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:二叉树下的不能相邻,求能 ...

  2. [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 ...

  3. 276. Paint Fence篱笆涂色

    [抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...

  4. [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 ...

  5. 【LeetCode】276. Paint Fence 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. LeetCode Paint Fence

    原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...

随机推荐

  1. PE格式的理解(待补充)

    PE文件格式 一.基本结构 1.DOS头一般到节区头成为PE头部分,其下称为PE体.文件的内容一般可分为代码(.text).数据(.data).资源(.rsrc),分别保存. 2.PE头与各节区的尾部 ...

  2. ios-UIWebView中js和oc代码的互调

    webview是ios中显示远程数据的网页控件,webview能显示的内容很多,MP4.文本.pdf等等: 关于js和oc代码的互相调用 1:oc中调用js代码; >>oc中调用js代码很 ...

  3. window窗口-button(按钮)-dialog(对话框,带按钮)

    描述:一个可拖动的窗口程序,默认情况下窗口自由移动.调整大小.打开关闭! 案例1(普通的窗口): <div class="easyui-window" icon-Cls=&q ...

  4. 【转】android ListView 几个重要属性

    android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...

  5. hadoop之JobTracker功能分析

    JobTracker是整个MapReduce计算框架中的主服务,相当于集群的“管理者”,负责整个集群的作业控制和资源管理.本文对JobTracker的启动过程及心跳接收与应答两个主要功能进行分析. 1 ...

  6. Entity Framework走马观花之把握全局

    在深入学习某项技术之前,应该努力形成对此技术的总体印象,并了解其基本原理,本文的目的就在于此. 一.理解EF数据模型 EF本质上是一个ORM框架,它需要把对象映射到底层数据库中的表,为此,它使用了三个 ...

  7. hdu 3572 Task Schedule 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572 Our geometry princess XMM has stoped her study i ...

  8. JAVA Hashmap不能用基本的数据类型

    今天开始学习Java... 转载:http://moto0421.iteye.com/blog/1143777 今天试了一下HahsMap, 采用如下形似定义 (这个下面是用了csdn的一位同仁的文章 ...

  9. win7 IIS7 PHP环境配置

    PHP5.2.17 官方下载: http://windows.php.net/downloads/releases/php-5.2.17-Win32-VC6-x86.zip PHP5.3.5 官方下载 ...

  10. 无法解决 equal to 运算中 &quot;Chinese_PRC_CI_AS&quot; 和 &quot;SQL_Latin1_General_CP1_CI_AS&quot; 之间的排序规则冲突。

    什么是排序规则(collation) 关于SQL Server的排序规则,估计大家都不陌生,在创建数据库时我们经常要选择一种排序规则(conllation),一般我们会留意到每一种语言的排序规则都有许 ...