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.

思路:DP。

设p(i)表示从第一张海报到第i张海报的情况数。

初始情况:p(0) = 0,p(1) = k。

当n=2时,有两种情况:1)第二张海报的颜色与第一张不同,一共k * (k - 1)种情况;2)两张海报的颜色一样,一共k种情况。所以p(2) = k + k* (k - 1).

当n>2时,假设当前是第i张海报,此时有两种情况:1)第i张海报与前一张的颜色不同,一共是p(i - 1) * (k - 1)种情况; 2) 第i张海报与前一张的颜色一样,一共是p(i-2) * (k - 1)种情况。

所以p(i) = p(i-2)*(k-1) + p(i-1)*(k-1).

在实现时,我们并不需要用数组来记录所有的结果,我们只需要p(i-2)和p(i-1)的值,因此可以用变量slow来记录p(i-2)的值,用fast来记录p(i-1)的值。

 class Solution {
public:
int numWays(int n, int k) {
if (n == ) return n;
if (n == ) return k;
int slow = k;
int fast = slow + slow * (k - );
for (int i = ; i <= n; i++) {
int cur = slow * (k - ) + fast * (k - );
slow = fast;
fast = cur;
}
return fast;
}
};

Paint Fence -- LeetCode的更多相关文章

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

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

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

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

  5. LeetCode Paint Fence

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

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

  8. 【LeetCode】276. Paint Fence 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

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

随机推荐

  1. 【Python】python内置函数、列表生成式、生成器

    一.内置函数 1 print(all([1,2,3,4]))#判断可迭代的对象里面的值是否都为真 2 print(any([0,1,2,3,4]))#判断可迭代的对象里面的值是否有一个为真 3 pri ...

  2. 剑指offer-替换空格02

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. class Solution: ...

  3. htmlagilitypack解析html

    这是个很好的的东西,以前做Html解析都是在用htmlparser,用的虽然顺手,但解析速度较慢,碰巧今天找到了这个,就拿过来试,一切出乎意料,非常爽,推荐给各位使用. 下面是一些简单的使用技巧,希望 ...

  4. 真的讨厌ClickOnce这东西

    ClickOnce真的问题多. 升级问题,每次升级后都新建一个文件夹,旧的程序数据全都没有.迁移过来也一堆问题.还有录音问题,Win7下录音报错,最后查来查去就是将文件路径太太长,要控制长度.还有安装 ...

  5. c#中获得MD5字符串方法

    在用户登录的过程中,我们会遇到要查询对比用户名密码的是否存在或者是否正确,但是数据库中存放的是通过MD5加密的字符串,所有我们可以先把用户输入的用户名或者是密码先转为DM5字符串再跟数据库查出的MD5 ...

  6. HTTPS和HTTP的区别:

    https协议需要到ca申请证书,一般免费证书很少,需要交费.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议http和https使用的是完全不同的连接方式用的 ...

  7. nginx的入门到框架设计

    mac上安装nginx 安装与启动 安装 brew install nginx 命令 通过 Homebrew 下载的软件默认位置在 /usr/local/Cellar 应该ln-s 加连接就能全局 n ...

  8. 【bzoj5047】空间传送装置 堆优化Dijkstra

    题目描述 n个点e条边的有向图,每条边是m种类型之一.第i种类型在第x时刻通过所花费的时间为$(a_i*x+b_i)\mod c_i+d_i$.可以在某个点停留.问:在s时刻从1号点出发,到达每个点所 ...

  9. jqury关于cooke的操作写入cookie后只显示一次的DIV提示框代码

    有时候当用户登录系统后,需要给用户弹出提示框,但是不需要总是弹出来,在这里加入访问cookie来判断是否弹出过提示框,如果弹出过那么保存cookie,下次根据cookie是否存在来判断是否弹出 < ...

  10. HTML标签的使用要注意语义化

    语义化标签:你认为用什么标签最能描述这块内容,觉得这样表述更有意义,那么就可以使用这个标签. 现在的浏览器对CSS支持都挺完善的(不包括CSS3),讲究的是结构与表现相分离,结构与行为相分离,一个WE ...