原题链接在这里:https://leetcode.com/problems/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.

题解:

base case n == 1, 那么有k种图法. n==2时,若选择相同图法,有k种. 若不同图法,有k*(k-1)种方法,总共有sameColorLastTwo + diffColorLastTwo种方法。

DP时,当到了 i 时 有两种选择,第一种 i 和 i-1不同色,那么有 i-1的总共方法 * (k-1), 就是(sameColorLastTwo + diffColorLastTwo) * (k-1).

第二种用相同色,那么 i -1 和 i-2 必须用不同色, 就是i-1的diffColorLastTwo.

最后返回两种方法的和diffColorLastTwo + sameColorLastTwo.

注意check corner case, e.g. n == 0, return 0. k == 0, return 0.

Time Complexity: O(n). Space: O(1).                   

AC Java:

 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 = 3; i<=n; i++){
int temp = diffColorLastTwo;
diffColorLastTwo = (sameColorLastTwo + diffColorLastTwo) * (k-1);
sameColorLastTwo = temp;
}
return diffColorLastTwo + sameColorLastTwo;
}
}

类似Paint HouseHouse Robber.

LeetCode Paint Fence的更多相关文章

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

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

  3. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  4. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  5. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  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#276] Paint Fence

    Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...

  9. Paint Fence -- LeetCode

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

随机推荐

  1. SOAPUI测试步骤之流量控制(Conditional Goto)

    1. TestSteps流量控制 1.1.有条件转到一步步测试 Conditional Goto拥有任意数量的的XPath表达式伴随相应的目标测试步骤.这些被应用到在先前的采样测试的最近的响应; 配置 ...

  2. 【BZOJ1984】月下“毛景树” 树链剖分+线段树

    [BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...

  3. ExtJs文件上传(Ext.ux.form.FileUploadField)

    Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField, { /**  * @cfg {String} buttonText The b ...

  4. Nodejs-搭建Nodejs开发环境

    学习nodejs,需要一个好的开发工具,并不想用无智能提示和不友好格式的记事本编写 1. 从www.nodejs.org下载nodejs并安装到指定的目录. 2. 下载一个开发工具, 可以选择webs ...

  5. Windows下查看端口冲突的进程

    在tomcat部署中,经常遇到80端口被占用,下面总结了两条查看端口进程的方法. 查看端口方法: netstat -aon|findstr "80"   如图,使用80端口的进程列 ...

  6. 偶然的发现(与Code无关)

    最近做后台用户注册, 在考虑不使用验证码, 百度搜了一下看了看一些相关技术, 发现了个小说——[万恶的验证码], 看了挺搞笑分享一下:原文链接 万恶的验证码 前言: 传说中,它是最为邪恶的吸血鬼,它是 ...

  7. BZOJ3343: 教主的魔法 分块

    2016-05-28  10:27:19 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3343 比较显然的分块题,分块后块内排序,维护整块的附 ...

  8. C#文字样式

    [字体] 中文名称 英文名称宋体 SimSun黑体 SimHei微软雅黑 Microsoft YaHei微软正黑体   Microsoft JhengHei新宋体   NSimSun新细明体 PMin ...

  9. Java_动态加载

    Java类动态加载(一)——java源文件动态编译为class文件最近在做java动态加载这方面的工作,起初也遇到了很多困难.网上关于这方便的东西很零散,为了便于日后回过头来再看,于是我将这几天的心得 ...

  10. 转MongoDB 使用Skip和limit分页

    关于MongoDB 数据分页和排序 limit,skip用户的一些基础语句,介绍MongoDB 数据分页和排序实例方法. 使用Skip和limit可以如下做数据分页: Code: page1 = db ...