[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

可以2个柱子颜色相同,则起点从2开始, 之前的所有情况,包括0 和 1 都要分别列出来写

[思维问题]:

列了几步以后觉得列不完,没思路:从小到大,反正只有相同、不相同两种情况,分情况讨论就行了

[一句话思路]:

第三根柱子开始有讨论余地,从此开始进行分类讨论。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 定义了temp,忘记用了。要注意用temp

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

列不出来就分情况讨论

[复杂度]:Time complexity: O(n) Space complexity: O(1) 没有新建数组、链表,就是四则运算时,空间为1

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/**
* @param n: non-negative integer, n posts
* @param k: non-negative integer, k colors
* @return: an integer, the total number of ways
*/
public int numWays(int n, int k) {
//corner case: n = 0 or 1
if (n == 0 || k == 0) {
return 0;
}
if (n == 1) {
return k;
}
//ini 0,1th
int sameColors = k;
int differentColors = k * (k - 1);
//getsum from 2 to <n
for (int i = 2; i < n; i++) {
int temp = differentColors;
differentColors = (temp + sameColors) * (k - 1);
sameColors = temp;
}
//answer n- 1
return differentColors + sameColors;
}
}

最新文章

  1. 替换Jar包里文件
  2. LINQ to SQL语句(12)之Delete和使用Attach
  3. JS:XML
  4. C#打开文件对话框
  5. Knockout JS实现任务管理应用程序
  6. hdu 4165 dp
  7. C语言第六节基本运算符
  8. JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)
  9. FZU1608(线段树)
  10. 结合实际项目分析pom.xml
  11. jenkins gradle 编译遇到tomcat异常
  12. mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)
  13. Jenkins + Docker 持续集成
  14. Android Multimedia框架总结(二十四)MediaMuxer实现手机屏幕录制成gif图
  15. Struts,Spring,Hibernate三大框架的
  16. React(二)组件
  17. [MySQL]理解关系型数据库4个事务隔离级别
  18. Hadoop HA方案调研
  19. 创建含有多module的springboot工程(八)
  20. 20145329 《网络对抗技术》浏览器MS11_050安全漏洞攻击

热门文章

  1. Linux history时间用户ip设置
  2. Linux 制作补丁 打补丁 撤销补丁
  3. 贴几个erlang文档站点
  4. java代码------计算器核心位置添加
  5. spring-session之四:Spring Session下的Redis存储结构
  6. Echarts运用
  7. Linux下压缩与解压
  8. 安装SQL Servre2000时提示“command line option syntax error! type command /? for help”
  9. Android:不同drawable文件夹的区别
  10. win系统主机上的虚拟机NAT模式可修改3389端口做远程登录