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. mysql编译时报的一个警告warning: type-punning to incomplete type might break strict-aliasing rules,可能是bug

    cmake的时候报了一个警告: /softdb/mysql-5.5.37/storage/innobase/handler/ha_innodb.cc:11870: warning: type-punn ...

  2. IIS配置php运行环境默认加载的php.ini路径

    第一步: 把PHP的安装路径添加到环境变量Path中,右键 “我的电脑” -> 高级 -> 环境变量 -> 系统变量,追加 D:PHP-5.2.8\; 第二步: 新建“系统变量” P ...

  3. Oracle中“行转列”的实现方式

    在报表的开发当中,难免会遇到行转列的问题. 以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用“行转列”: scott的emp的原始数据为: EMPNO ENAM ...

  4. ADT开发AndroidManifest.xml file missing错误

    一个错误“AndroidManifest.xml file missing”但helloworld目录下有此文件,几番google仍没能解决.想起曾经在网络上看到的一个修复project的办法,抱着死 ...

  5. URL编码表%20Base64编码表%20HTTP消息含义

    URL编码表 backspace 8% A 41% a 61% § %A7 Õ %D5   tab 9% B 42% b 62% « %AB Ö %D6   linefeed %0A C 43% c ...

  6. loadrunner生成随机数

    loadrunner生成随机数一: 对网站注册进行压力测试时,需要对注册的用户名进行参数化,因为可以会用到大量的测试数据,所以选择通过生成随机数来进行参数化.最开始用loadrunner自带的参数随机 ...

  7. 利用pushState开发无刷页面切换

    转载:http://www.cnblogs.com/flash3d/archive/2013/10/23/3384823.html 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与 ...

  8. SpringMyBatis解析1-使用示例

    MyBatis使用介绍 MyBatis的详细使用介绍  http://www.cnblogs.com/xrq730/category/796495.html 建立PO public class Per ...

  9. DSP using Matlab 书内练习Example 2.1

    先上代码,后出结果图. a. n = [-5:5]; x=2*impseq(-2,-5,5) - impseq(4,-5,5); set(gcf,'Color',[1,1,1]) % 改变坐标外围背景 ...

  10. dubbo服务治理框架设计

    dubbo.JSF作为使用最广泛的服务端框治理架,其设计和实现思想值得进行学习研究. 整个服务管理框架核心的原理基于反射以及socket调用实现,服务管理框架包含服务的注册管理 服务的索引管理以及服务 ...