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程序执行时的不同电脑路径不同问题

    原因:因代码转移时项目路径发生了变化,导致解释器无法找到对应路径,是的程序无法正常执行 需求: 1.我希望代码能在不同的电脑下,不必修改源代码就能正常执行(所需模块已安装的前提下) 2.我希望代码在命 ...

  2. Python学习-django-ModelForm组件

    ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 labels=None, # 提示信 ...

  3. 内存cgroup

    内存cgroup的值都是从哪里来的呀 page_counter_charge是增加page_counter的计数, try_charge函数和mem_cgroup_migrate函数是增加普通进程内存 ...

  4. Android使用adb命令查看CPU信息

    Android中使用JNI编程的时候会需要编译出不同的SO文件,以供适配不同的机型. 例如: 由此需要查看不同机型的CPU信息. 使用ADB命令查看CPU信息命令如下: 1. adb shell 2. ...

  5. ES6 Destructuring Assignment All In One

    ES6 Destructuring Assignment All In One ES6 & Destructuring Assignment Axios, vue https://develo ...

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

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

  7. java链接数据库--Mysql

    /************************************************************************* > File Name: Mysql.jav ...

  8. 研华 FWA-3231 单路E3平台

    研华 FWA-3231 单路E3平台 服务器主板上芯片组与处理器的适配参考

  9. [CF191C]Fools and Roads

    题目大意:有一颗$n$个节点的树,$k$次旅行,问每一条被走过的次数. 题解:树上差分,$num_x$表示连接$x$和$fa_x$的边被走过的次数,一条路径$u->v$,$num_u+1,num ...

  10. golang effective 翻译

    参考 Effective Go 官方文档 其他参考译文 https://studygolang.com/articles/3228 http://docscn.studygolang.com/doc/ ...