*Paint Fence
2016-01-13 00:43
330 查看
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.
We divided it into two cases.
the last two posts have the same color, the number of ways to paint in this case issameColorCounts.
the last two posts have different colors, and the number of ways in this case isdiffColorCounts.
The reason why we have these two cases is that we can easily compute both of them, and that is all I do. When adding a new post, we can use the same color as the last one (if allowed) or different color. If we use different color, there're k-1 options, and the outcomes shoule belong to the diffColorCounts category. If we use same color, there's only one option, and we can only do this when the last two have different colors (which is the diffColorCounts). There we have our induction step.
Here is an example, let's say we have 3 posts and 3 colors. The first two posts we have 9 ways to do them, (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3). Now we know that
and
Now for the third post, we can compute these two variables like this:
If we use different colors than the last one (the second one), these ways can be added intodiffColorCounts, so if the last one is 3, we can use 1 or 2, if it's 1, we can use 2 or 3, etc. Apparently there are
If we use the same color as the last one, we would trigger a violation in these three cases (1,1,1), (2,2,2) and (3,3,3). This is because they already used the same color for the last two posts. So is there a count that rules out these kind of cases? YES, the diffColorCounts. So in cases withindiffColorCounts, we can use the same color as the last one without worrying about triggering the violation. And now as we append a same-color post to them, the former diffColorCountsbecomes the current sameColorCounts.
Then we can keep going until we reach the n. And finally just sum up these two variables as result.
Hope this would be clearer.
reference: https://leetcode.com/discuss/56173/o-n-time-java-solution-o-1-space
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.
public int numWays(int n, int k) { if(n == 0) return 0; else if(n == 1) return k; int diffColorCounts = k*(k-1); int sameColorCounts = k; for(int i=2; i<n; i++) { int temp = diffColorCounts; diffColorCounts = (diffColorCounts + sameColorCounts) * (k-1); sameColorCounts = temp; } return diffColorCounts + sameColorCounts; }
We divided it into two cases.
the last two posts have the same color, the number of ways to paint in this case issameColorCounts.
the last two posts have different colors, and the number of ways in this case isdiffColorCounts.
The reason why we have these two cases is that we can easily compute both of them, and that is all I do. When adding a new post, we can use the same color as the last one (if allowed) or different color. If we use different color, there're k-1 options, and the outcomes shoule belong to the diffColorCounts category. If we use same color, there's only one option, and we can only do this when the last two have different colors (which is the diffColorCounts). There we have our induction step.
Here is an example, let's say we have 3 posts and 3 colors. The first two posts we have 9 ways to do them, (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3). Now we know that
diffColorCounts = 6;
and
sameColorCounts = 3;
Now for the third post, we can compute these two variables like this:
If we use different colors than the last one (the second one), these ways can be added intodiffColorCounts, so if the last one is 3, we can use 1 or 2, if it's 1, we can use 2 or 3, etc. Apparently there are
(diffColorCounts + sameColorCounts) * (k-1)possible ways.
If we use the same color as the last one, we would trigger a violation in these three cases (1,1,1), (2,2,2) and (3,3,3). This is because they already used the same color for the last two posts. So is there a count that rules out these kind of cases? YES, the diffColorCounts. So in cases withindiffColorCounts, we can use the same color as the last one without worrying about triggering the violation. And now as we append a same-color post to them, the former diffColorCountsbecomes the current sameColorCounts.
Then we can keep going until we reach the n. And finally just sum up these two variables as result.
Hope this would be clearer.
reference: https://leetcode.com/discuss/56173/o-n-time-java-solution-o-1-space
相关文章推荐
- jQuery页面加载时触发ready()事件 dom结构加载完成就触发。(参考慕课网)
- jQuery 使用remove()和empty()方法删除元素(参考慕课网)
- [Cousera Angular JS学习笔记]第一周(2)
- jQuery each 的使用(参考慕课网)
- 浅谈Angular的 $q, defer, promise
- 网页端压缩解压缩插件JSZIP库的使用
- javascript图片切换综合实例(循环切换、顺序切换)
- 详解JavaScript中localStorage使用要点
- 分享javascript计算时间差的示例代码
- jquery对复选框(checkbox)的操作汇总
- JavaScript类型检测之typeof 和 instanceof 的缺陷与优化
- Jquery uploadify上传插件使用详解
- 浅析JavaScript中的变量复制、参数传递和作用域链
- 基于jquery实现的仿优酷图片轮播特效代码
- 轻松实现javascript图片轮播特效
- JSON+Jquery省市区三级联动
- javascript中的作用域和闭包详解
- AngularJS单选框及多选框实现双向动态绑定
- jQuery简单操作cookie的插件实例
- js和jquery实现监听键盘事件示例代码