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. hp惠普服务器监控硬盘

    惠普 hpssacli 工具使用 查看raid卡信息(包括控制器状态.Cache状态.电池状态) # hpssacli ctrl all show status 查看raid详细信息 # hpssac ...

  2. Hadoop书籍汇总

    <Hadoop实战>陆嘉恒 <Hadoop - The Definitive Guide>Tom White,中文版<Hadoop权威指南> <Hadoop技 ...

  3. c#不重复的排序方法

    public int getRandom(int num) { Thread.Sleep(5); // Random ro = new Random(unchecked((int)DateTime.N ...

  4. C# 的可空合并运算符(??)到底是怎样的宝宝?

    前言废语 也怪自己小白和不勤奋,没有系统的学习C#相关的东西,工作一年多还是初级小菜,深感不安,来到园子才发现好多钻研技术的人,也渐渐发现自己开始喜欢上了这个编程的世界.今日偶遇??操作符,发现我只看 ...

  5. HTML+CSS基础学习笔记(1)

    一.了解HTML.CSS.JS 1.HTML是网页内容的载体. 内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2.CSS样式是表现. 用来改变内容外观的东西称之为表现 ...

  6. gitcafe 使用hexo搭建博客

    --缘由:因为看大家都用github等搭建博客,作为半个程序员的自己,也按捺不住了,终于有空来尝试一把了,选择了和github 相同功能的gitcafe网站,因为在国内比较快,这是大家的看法,下面写一 ...

  7. Attribute 特性

    转载   不错   摘要:纠结地说,这应该算是一篇关于Attribute 的笔记,其中的一些思路和代码借鉴了他人的文笔(见本文底部链接).但是,由于此文对Attribute 的讲解实在是叫好(自夸一下 ...

  8. (七)Struts2 验证框架

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 验证简介 Struts2 基于Struts2 拦截器,为 ...

  9. 01-Objective-C

    前言   目 前来说,Objective-C(简称OC)是iOS开发的核心语言,在开发过程中也会配合着使用C语言.C++,OC主要负责UI界面,C语言.C++ 可用于图形处理.近来,流传Ruby.C# ...

  10. jQuery 遍历祖先

    祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...