原题链接在这里: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. BZOJ3723 : PA2014Final Gra w podwajanie

    暴力搜索出所有可行的形状,可以发现本质不同的形状数只有6000个左右. 对于每个形状,它的大小不超过$8\times 8$,故可以按照右下角为原点重建坐标系,用一个unsigned long long ...

  2. [转]crontab命令指南

    原文链接:http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html 前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划 ...

  3. 学习编写Windows Live Writer插件

    1.参考链接 参考:http://www.cnblogs.com/liulun/archive/2009/05/27/1491116.html Live Writer API参考:http://msd ...

  4. JS:操作样式表1:行内样式

    //访问元素样式1, stye属性只对行内样式有用 var box = document.getElementById("box"); // alert(box.style.col ...

  5. UVA 11489 - Integer Game(找规律)

    题目链接 #include <cstdio> #include <cstring> #include <string> #include <cmath> ...

  6. Android -- 简单广播接收与发送(1)

    1. 效果图

  7. 升级OS X EI Capition 版本导致cocoapods 使用终端上pod: command not found

    1)安装过cocoapods, 那么输入 : sudo gem install -n /usr/local/bin cocoapods 当然 上个步骤解决了我的 难题 2)首先在终端输入 gem so ...

  8. 数据库存储ATM机,开户、查询等信息

    package com.bank.unionpay; //银行卡的接口 public interface I_yinhangka { //抽象方法 //public abstract默认修饰抽象的 p ...

  9. Hive_进阶

    回顾: hive 优点 1. 类sql语句靠近关系型数据库,可自定义函数,增加了扩展性,易于开发,减少mapreduce学习成本 2. hive转换sql语句为mapreduce程序以mapreduc ...

  10. fn标签常用方法使用说明

    需要jstl.jar包,然后在jsp页头导入 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix=& ...