276. Paint Fence
来源:互联网 发布:js 浮点数计算 编辑:程序博客网 时间:2024/06/06 17:33
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.
1、prediff = currdiff,presame = cursame
2、currdiff = (prediff + presame)* (k - 1)
3、currsame = prediff
代码实现如下:
public class Solution { public int numWays(int n, int k) { if (n == 0 || k == 0) { return 0; } if (n == 1) { return k; } int currsame = k; int currdiff = k * (k - 1); for (int i = 2; i < n; i ++) { int prediff = currdiff; int presame = currsame; currdiff = (prediff + presame) * (k - 1); currsame = prediff; } return currdiff + currsame; }}
0 0
- LeetCode 276. Paint Fence
- LeetCode 276. Paint Fence
- 276. Paint Fence
- 【dp】leetcode 276. Paint Fence
- Paint Fence
- Paint Fence
- [leetcode] 276. Paint Fence 解题报告
- LeetCode 276. Paint Fence(篱笆涂色)
- Leetcode 276. Paint Fence (经典DP)
- Leetcode 276. Paint Fence (Easy) (cpp)
- Leetcode——276.Paint Fence
- [Leetcode] 276. Paint Fence 解题报告
- **LeetCode-Paint Fence
- [leetcode 276] Paint Fence
- [LeetCode276]Paint Fence
- leetcode Paint Fence
- Paint Fence 栅栏涂色
- Paint fence and house
- C语言中static变量详解
- 20170122【比赛】总结
- qt5打开qt4项目时出错及解决办法
- 2016-2017寒假学习计划
- Eclipse中安装Spring IDE
- 276. Paint Fence
- 总结项目使用一个TabLayout选项卡
- Sass-四种编译输出代码风格
- 微信小程序教程(第三篇)
- 超时问题总结
- Java中将数组转化为List
- libpcap的一些主要函数和结构介绍
- FIX POSTFIX "FATAL: OPEN DATABASE /ETC/ALIASES.DB: NO SUCH FILE OR DIRECTORY" ERROR
- Ubuntu 16上装完nodejs还是找不到node命令?