Problem:

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.

Wrong Solution:

public class Solution {
public int numWays(int n, int k) {
if (n == 0 || k == 0)
return 0;
if (n == 1)
return k;
int total = k;
int count = 1;
while (count < k) {
total *= k - 1;
count++;
}
return total;
}
}

Mistake Analysis:

Fail to throughly understand problem!!! The problem asks us to compute the number of ways for arranging the painting which has no more than two sussive post share the same color. It means we were allowed to make two neighboring posts share the same color!

Analysis:

The problem of asking how many ways to do something is usually very easy!
And it could always be solved through dynamic programming. You just need to carefully design the transitional function acoording to characteristics or certain restrictions. We know for each post, it could differ or same as its previous post's color.
Assume:
differ_count: represents the current post with different color with its previous post(the painting ways)
same_count: represents the current post share the same color with its previous post(the painiting ways) We could have following trasitinao function
differ_count(i) = differ_count(i-1) * (k-1) + same_count(i-1) * (k-1)
same_count(i) = differ_count(i-1) //cause the current post must have the same color with post i-1, thus we could only use the way that differ_count(i-1) Base case:
2 is a perfect base case for use to start, since it has simple same_count and differ_count;

Solution:

public class Solution {
public int numWays(int n, int k) {
if (n == 0 || k == 0)
return 0;
if (n == 1)
return k;
int same_count = k;
int differ_count = k * (k - 1);
for (int i = 3; i <= n; i++) {
int temp = differ_count;
differ_count = differ_count * (k - 1) + same_count * (k - 1);
same_count = temp;
}
return same_count + differ_count;
}
}

[LeetCode#276] Paint Fence的更多相关文章

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

  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】276. Paint Fence 解题报告(C++)

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

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

  5. [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

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

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

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

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

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

随机推荐

  1. Eclipse将项目部署tomcat的webapps目录

      Eclipse将项目部署tomcat的webapps目录 >>>>>>>>>>>>>>>>>& ...

  2. 功能点分析法FPA笔记

    转载请注明出处:http://www.cnblogs.com/lidabnu/p/5700412.html 主要参考资料来自百度文库:http://wenku.baidu.com/link?url=y ...

  3. OC基础-day04

    #pragma mark -  Day04_01_匿名对象 1. 如果函数有返回值 我们可以不使用变量接收返回值. 而是直接将函数写在要使用其返回值的地方. 2. 正常情况下.我创建对象. 是使用了1 ...

  4. 开源字体包缩减font-spider

    最近公司产品用微软雅黑,被告~  领导要求产品后台系统必须更换字体,美工给了个宋体,个人觉得太丑,就网上搜了些处理方法使用 CSS属性 @font-face 自定义字体, 最后决定用 思源黑体 (思源 ...

  5. Java之循环输出等腰三角形

    public class aaa{ public static void main(String[] args) { int max=5; for(int i=1;i<=5;i++){//控制行 ...

  6. 寒假的ACM训练(一)

    今天开始ACM训练,选择了刘汝佳的<挑战编程>,暂时算是开始了. 测评的网址: http://www.programming-challenges.com 第一个题目是水题啦.3n+1. ...

  7. cocos2dx 实现不一样的ScrollView

    原来在公司被迫加班加点赶工,用lua实现的版本:http://www.cnblogs.com/mmc1206x/p/4146911.html 后来因我个人的需要, 用C++实现了一个版本. 蓦然回首, ...

  8. State 模式

    State模式中我们将状态逻辑和动作实现进行分离.允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类:在一个状态即将结束的时候启用下一个状态. /////////state.h// ...

  9. 如何给html元素的onclick事件传递参数(即如何获取html标签的data-*属性)

    现在做的一个小系统为了达到领导所说的很炫的效果有用到Metro UI CSS,但是因为如何给每个磁贴(div标签)的click事件传递参数折腾了蛮久(偶是菜鸟),后来终于找到一个解决方案即通过data ...

  10. mouseover和mouseout事件在鼠标经过子元素时也会触发

    JavaScript的mouseover和mouseout事件,在绑定元素内部有子元素的情况下, 经过绑定元素时会多次触发mouseover和mouseout事件. jQuery解决办法: jquer ...