题目:

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. MyEclipse导入jquery等文件报错的解决方案

    1.选中报错的jquery文件例如“jquery-1.8.0.min.js”. 2.右键选择 MyEclipse-->Exclude From Validation . 3.再右键选择 MyEc ...

  2. 20145120 《Java程序设计》第8周学习总结

    20145120 <Java程序设计>第8周学习总结 教材学习内容总结 NIO使用频道(channel)来衔接数据节点 read()将ReadableByteChannel中的数据读至By ...

  3. Html5 Canvas动画旋转的小方块;

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  4. saltstack知识点

    1.salt '*' sys.doc 显示minion设备上支持的命令   2.salt-call 可以显示比salt更多的信息,可以用来调试,检查,如果需要更详细的信息使用 salt-call -l ...

  5. SQL Server 执行计划中的扫描方式举例说明

    SQL Server 执行计划中的扫描方式举例说明 原文地址:http://www.cnblogs.com/zihunqingxin/p/3201155.html 1.执行计划使用方式 选中需要执行的 ...

  6. C++ Templates基本知识

    一.使用Templates的原因:例如我们要写一个算法,由于类型不同,我们不得不做一下工作.1.使用宏定义代替Templates不利于类型检查. 2.将一些算法放在基类里,以后的扩展的子类都需要充基类 ...

  7. phonegap/cordova常用命令

    创建项目 cordova create foldername com.wps.test projectName cd foldername 基本设备信息 设备 API: cordova plugin ...

  8. 用Time Machine做更换电脑工具

    简介: Time Machine这个工具,是直接备份硬盘上的内容.所以,它是直接有备份系统的. 准备: 1.准备一个移动硬盘,存贮空间大于你需要备份系统空间 操作流程: 1. 用Disk Utilit ...

  9. iis7/7.5设置上传文件最大大小

    本编今天接到一个客户的修改,说一个68M的pdf文件上传不上去,但是我本地开启断点调试了好几遍,都没有问题,能正常上传文件,由此确定不是代码问题.然后我试着上传5M左右的pdf却能正常的上传,然后上传 ...

  10. C# 模拟一个处理消息队列的线程类 Message Queue

    // 模拟一个处理消息队列的类 class MessageHandler { // 消息队列 private Queue<string> messageQue = new Queue< ...