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.

分析:

  这题看起来能直接用公式输出答案吧...如果觉得公式算起来麻烦,那么对于连续排列串,而且只需要得到可以涂的种类数,故应可用动态规划解决。

代码:

int totalways(int n, int k) {
if(n == || n == || k == )
return k;
int ls = k, ld = k * (k - );
for(int i = ; i < n; i++) {
int temp = ls;
ls = ld;
ld = (temp + ld) * (k - );
}
return ls + ld;
}

[Locked] Paint Fence的更多相关文章

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

  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 Fence 粉刷篱笆

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

  4. LeetCode Paint Fence

    原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...

  5. 276. Paint Fence

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

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

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

  9. 276. Paint Fence篱笆涂色

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

随机推荐

  1. Android学习笔记(广播机制)

    1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...

  2. iOS里面消除使用代理调用方法时间警告问题

    iOS里面有三种调用函数的方式: 直接调用方法   [对象名 方法]; performselector:    [对象名 perform方法]; NSInvocation     调用 在使用代理调用 ...

  3. 仿照淘宝首页做的一个高度伪对齐demo

    功能就是当右边高度没有左边高的情况下做的一些处理,由于本人技术有限,不兼容所有浏览器, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...

  4. Bootstrap_Javascript_固定定位

    Affix 插件可以对任何元素进行固定定位,其中比较简单的方法,就是通过自定义属性 data 来触发.其主要包括两个参数: 1.data-spy:取值 affix,表示元素固定不变的. 2.data- ...

  5. js打开新的窗体不被浏览器阻止

    转载自js弹出新窗口而不会被浏览器阻止的方法有时候希望可以用js另开新窗口,但用window.open方法打开窗口总是被浏览器阻止, 可以用下面的方法打开新窗口而不会遭到拦截 1.新添加一个Form ...

  6. 解决xp共享的批处理文件

    在空白地方点右键选择新建一个文本文档,将默认的“新建 文本文档.txt”文件名改名为以下红色加粗字体内容,再复制红色内容以下的黑字部分到改名后的文档.其他文件生成的方法相同.完成后根据需要双击CMD扩 ...

  7. PHP要注意的14个问题

    1.页面之间无法传递变量 get,post,session 在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使 用$_GET['foo'],$_POST['foo'],$ ...

  8. C语言结构体(struct)使用方法

    基本定义:结构体,通俗讲就像是打包封装,把一些变量有共同特征(比如同属于某一类事物的属性)的变量封装在内部,通过一定方法访问修改内部变量. 结构体定义: 第一种:只有结构体定义 struct stuf ...

  9. bzoj1311: 最优压缩

    Description  其中: Auv是与Aij相邻的像素(为了简化,认为(i-1,j),(i+1,j,(i,j-1),(i,j+1)为相邻元素); Wij取值0或者1,表示Aij修改后取V0或者V ...

  10. JS:window.onload的使用介绍

    作者: 字体:[增加 减小] 类型:转载 时间:2013-11-13我要评论 window.onload在某些情况下还是比较实用的,比如加载时执行哪些脚本等等,下面有几个不错的示例,需要的朋友可以参考 ...