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 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 都要分别列出来写
[思维问题]:
列了几步以后觉得列不完,没思路:从小到大,反正只有相同、不相同两种情况,分情况讨论就行了
[一句话思路]:
第三根柱子开始有讨论余地,从此开始进行分类讨论。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 定义了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;
}
}
276. Paint Fence篱笆涂色的更多相关文章
- 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 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 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- 【LeetCode】276. Paint Fence 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [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 ...
- 【BZOJ-1260】涂色paint 区间DP
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1147 Solved: 698[Submit][Sta ...
- 【DP】BZOJ 1260: [CQOI2007]涂色paint
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 893 Solved: 540[Submit][Stat ...
- BZOJ 1260: [CQOI2007]涂色paint( 区间dp )
区间dp.. dp( l , r ) 表示让 [ l , r ] 这个区间都变成目标颜色的最少涂色次数. 考虑转移 : l == r 则 dp( l , r ) = 1 ( 显然 ) s[ l ] = ...
- BZOJ_1260_[CQOI2007]涂色paint _区间DP
BZOJ_1260_[CQOI2007]涂色paint _区间DP 题意: 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字 ...
随机推荐
- STMM32 ‘&’ 操作
if(0x04 == (new_cfg&0x04)){ sys_cfg_msg.pps_cfg = ; cn_save_data[cn_save_index_stp].hash= ; sys_ ...
- Virsh中创建虚拟机两种方式define和create的区别
本质上两者一样的,都是从xml配置文件创建虚拟机 define 丛xml配置文件创建主机但是不启动 create 同样是丛xml配置文件创建主机,但是可以指定很多选项,比如是否启动,是否连接控制台 ...
- Spark性能优化:资源调优篇(转)
在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...
- 基于Oracle的EntityFramework的WEBAPI2的实现(三)—— 建立APIController及设置返回类型JSON、XML等
建立普通的ApiControler 右击项目中的controller文件夹·添加·控制器·包含操作的webapi2控制器(使用entity framework),写个名字,如果:Test.然后选择类, ...
- 解决Intel SSD 330i 240G在Windows 8启动慢的问题
笔者半年前更新了一下老迈的Thinkpad T410i笔记本电脑,在美国亚马逊海购了Intel SSD 330i 240G.(PS:购入价USD 129.99 ,现在最新的335i 240G差不多要U ...
- MySQL查询优化器工作原理解析
手册上查询优化器概述 查询优化器的任务是发现执行SQL查询的最佳方案.大多数查询优化器,包括MySQL的查询优化器,总或多或少地在所有可能的查询评估方案中搜索最佳方案.对于联接查询,MySQL优化器所 ...
- Hive使用入门
Hive简介 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成 ...
- jquery Autocomplete函数
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 在centos7上搭建mongodb副本集
1.安装副本集介绍 副本集(Replica Set)是一组MongoDB实例组成的集群,由一个主(Primary)服务器和多个备份(Secondary)服务器构成.通过Replication,将数据的 ...
- Linux无法登录,显示module is unknown,一闪而过
1.使用单用户模式登录系统(不作介绍) 2.查看日志:vim /var/log/secure 3.记忆起曾经配置oracle添加过该参数 vim/etc/pam.d/login中加入了: sessio ...