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
分析:

这种给定一个规则,计算有多少种结果的题目一般都是动态规划,因为我们可以从这个规则中得到递推式。根据题意,不能有超过连续两根柱子是一个颜色,也就意味着第三根柱子要么跟第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色。如果不是同一个颜色,计算可能性的时候就要去掉之前的颜色,也就是k-1种可能性。假设dp[1]是第一根柱子及之前涂色的可能性数量,dp[2]是第二根柱子及之前涂色的可能性数量,则dp[3]=(k-1)*dp[1] + (k-1)*dp[2]。

另一种理解是第三根柱子只有两种选择:跟第二根一个颜色dp[1]*(k-1),跟第二根不同颜色dp[2]*(k-1), 这两种情况是不互相包含的,是独立的,

     public int numWays(int n, int k) {
// 当n=0时返回0
int dp[] = {, k , k*k, };
if(n <= ){
return dp[n];
}
for(int i = ; i <= n; i++){
// 递推式:第三根柱子要么根第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色
dp[] = (k - ) * (dp[] + dp[]);
dp[] = dp[];
dp[] = dp[];
}
return dp[];
}

Reference:

https://segmentfault.com/a/1190000003790650

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

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

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

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

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

  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. 第一周:通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的

    姓名:吕松鸿 学号:20135229 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/U ...

  2. [转帖]第二个显示屏上禁用Windows任务栏

    http://os.51cto.com/art/201812/589207.htm 这个过程非常简单,你可以在一分钟内摆脱第二个屏幕上的任务栏. 您需要做的就是按照以下步骤操作: --打开设置,然后转 ...

  3. [转帖]Windows10七大版本区别在哪?

    Windows10七大版本区别在哪? http://os.51cto.com/art/201804/570132.htm 一.Windows10家庭版 对于绝大多数用户来说,最后可能获得的应该就是Wi ...

  4. Win2008r2 设置 多用户同时远程

    Study From http://blog.sina.com.cn/s/blog_7ebe66420101tfln.html 1. 启动远程桌面,关闭防火墙 略过不提 2. 添加远程服务角色, 打开 ...

  5. OneZero第五周第一次站立会议(2016.4.18)

    1. 时间: 13:00--13:15  共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  6. IdeaVim-常用操作(转载)

    IdeaVim简介 IdeaVim是IntelliJ IDEA的一款插件,他提高了我们写代码的速度,对代码的跳转,查找也很友好. 安装位置 安装之后它在 Tools > Vim Emulator ...

  7. code review & github

    code review & github code review https://github.com/features/code-review/ https://github.com/mar ...

  8. delphi中登录界面关闭直接现实主界面是怎么回事?

    修改工程文件: Application.Initialize; Application.CreateForm(TmainForm, mainForm); Application.ShowMainFor ...

  9. 如何把EntityList转换成DataSet

    public static DataSet ToDataSet<TSource>(this IList<TSource> list) { Type elementType = ...

  10. Java 基本数据类型 sizeof 功能

    Java 基本数据类型 sizeof 功能 来源 https://blog.csdn.net/ithomer/article/details/7310008 Java基本数据类型int     32b ...