原题链接在这里: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. BZOJ3145 : [Feyat cup 1.5]Str

    如果不存在模糊点,那么答案就是两个串的最长公共子串. 如果模糊点是某个串的开头或者结尾,那么可以暴力枚举另一个串中的某个前后缀更新答案. 否则,假设模糊点在第一个串里是$i$,在第二个串里是$j$,那 ...

  2. BZOJ2990 : [Ontak2010]Keyboard

    考虑从$(1,1)$开始搜索移动方案,每次移动坐标的变化量都是$2$. 如果构成了环,那么环的周长肯定是偶数. 考虑这个环一定要被若干个骨牌覆盖,且还有一个位置是空的. 所以得出环的周长是奇数,矛盾, ...

  3. 20145308刘昊阳 《Java程序设计》实验五报告

    20145308刘昊阳 <Java程序设计>实验五 Java网络编程及安全 实验报告 实验名称 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: ...

  4. 【BZOJ】1406: [AHOI2007]密码箱

    http://www.lydsy.com/JudgeOnline/problem.php?id=1406 题意:求$0<=x<n, 1<=n<=2,000,000,000, 且 ...

  5. [BZOJ2793][Poi2012]Vouchers

    2793: [Poi2012]Vouchers Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 316  Solved: 148[Submit][Stat ...

  6. Android --日期控件使用并显示选择的日期

    1. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  7. FS_11C14温湿度传感器(二)

    作者:刘老师,华清远见嵌入式学院讲师. 在FS_11C14平台DHT11传感器程序: /******************************************************** ...

  8. SVN文本文件报二进制属性的问题

    2011-11-21 00:42 svn总出现二进制相关的烦人事 在使用svn add 时提示: A  (bin)  templates/translate/screen/selectTransLan ...

  9. [zt]OJ常见的Judge Status

    Queuing : 提交太多了,OJ无法在第一时间给所有提交以评判结果,后面提交的程序将暂时处于排队状态等待OJ的评判.不过这个过程一般不会很长. Compiling : 您提交的代码正在被编译. R ...

  10. SBT Assembly - Deduplicate error & Exclude error

    sbt assembly java.lang.RuntimeException: deduplicate: different file contents found in the following ...