[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 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的更多相关文章
- [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 ...
- 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:二叉树下的不能相邻,求能 ...
- 【LeetCode】276. Paint Fence 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 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 ...
- [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 ...
- 276. Paint Fence篱笆涂色
[抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 程序员带你十天快速入门Python,玩转电脑软件开发(二)
关注今日头条-做全栈攻城狮,学代码也要读书,爱全栈,更爱生活.提供程序员技术及生活指导干货. 如果你真想学习,请评论学过的每篇文章,记录学习的痕迹. 请把所有教程文章中所提及的代码,最少敲写三遍,达到 ...
- jquery 对select option 增删改查
一.查 jQuery获取select的Text和Value: 代码如下: 1.当select添加选择事件,当选择其中一项时触发: $("#select_id"). ...
- (八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...
- MyEclipse中配置自己的JRE和tomcat
MyEclipse中配置自己的JRE:windows>Preference>java>Installed JREs>Add>Stantard VM>next> ...
- Codevs 1065 01字符串
1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且其中不能含有 ...
- jquery ajax 后台响应成功,返回正确json但不执行success方法,执行error的问题
昨天被这问题卡了好几个小时.查看http状态码:是200.而且返回了预想的json字符串.但执行的是error方法,不执行success方法.在网上查了一下,才发现是后台页面返回的json字符串格式不 ...
- Deep Belief Network
Deep Belief Network3实例3.1 测试数据按照上例数据,或者新建图片识别数据. 3.2 DBN实例//****************例2(读取固定样本:来源于经典优化算法测试函数S ...
- linux自定义开机启动服务
转 http://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html 手工创建服务 1.在/etc/rc.d/init.d目录下创建shel ...
- Python 基础-python-列表-元组-字典-集合
列表格式:name = []name = [name1, name2, name3, name4, name5] #针对列表的操作 name.index("name1")#查询指定 ...
- 小A老空调需求管理小记
1. 关注一个动作的流程和内容 比如登录,我需要提供的是登录接口,但是登录是否只是一个动作?一个动作一定不完整,还应该包括动作的内容和步骤(流程),这里有两个问题:就是落地和把问题想复杂了:其实登录还 ...