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.

Notice

n and k are non-negative integers.

Example
Given n=3, k=2 return 6

post 1, post 2, post 3
way1 0 0 1
way2 0 1 0
way3 0 1 1
way4 1 0 0
way5 1 0 1
way6 1 1 0

LeetCode上的原题,请参见我之前的博客Paint Fence

class Solution {
public:
/**
* @param n non-negative integer, n posts
* @param k non-negative integer, k colors
* @return an integer, the total number of ways
*/
int numWays(int n, int k) {
int same = , diff = k;
for (int i = ; i <= n; ++i) {
int t = diff;
diff = (same + diff) * (k - );
same = t;
}
return same + diff;
}
};

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

  3. [LintCode] Paint House 粉刷房子

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

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

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

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

  7. [Swift]LeetCode276. 粉刷栅栏 $ Paint Fence

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

  8. 276. Paint Fence篱笆涂色

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

  9. [LintCode] 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 ...

随机推荐

  1. memcpy与memmove的区别

    在面试中经常会被问道memcpy与memove有什么区别? 整理如下: 其实主要在考C的关键字:restrict C库中有两个函数可以从一个位置把字节复制到另一个位置.在C99标准下,它们的原型如下: ...

  2. [荐]SWFObject 2最新版语法调用示例

    我一直都在用SWFObject 插入flash,好处多多,代码简洁,不会出现微软的“单击此处以激活控件”的提示(据可靠消息,这个是微软惹的官司,其结果是害苦了用户).不过先前的 调用方法着实有些繁琐, ...

  3. UE对一个很长的字符按16位自动换行

    ① ②设定16个位 ③ ④结果

  4. android:layout_gravity和android:gravity属性的区别(转)

    gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在view的什么位置,默认值 ...

  5. jquery中append()、prepend()、after()、before()的区别详解

    append() - 在被选元素的结尾插入内容(内容的结尾,比如说有个a标签,则是在</a>这个标签之前添加东西) prepend() - 在被选元素的开头插入内容(内容的开始,比如说有个 ...

  6. HTML概况性介绍

    HTML(HyperText Markup Language)汉语的意思是:超文本标记语言. ”超文本”是指.html页面内不仅仅可以包含文字,还可以包含图片.链接,甚至音乐.程序等非文字元素. “标 ...

  7. Strust2 初体验

    Struts2的第一个案例 首先我们需要引入架包 entity: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package ...

  8. Linux上部署JProbe

    1.在本机PC上安装JProbe: JProbeforWindowsEXEFormat_810

  9. Tomcat不输入项目名进入自己项目(根目录指向自己的项目)

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...

  10. TweenMax_API介绍

    构造函数:TweenMax(target:Object, duration:Number, vars:Object) target:Object -- 需要缓动的对象 duration:Number ...