[抄题]:

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;
}
}

276. Paint Fence篱笆涂色的更多相关文章

  1. 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 ...

  2. 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:二叉树下的不能相邻,求能 ...

  3. [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 ...

  4. 【LeetCode】276. Paint Fence 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  5. [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 ...

  6. 【BZOJ-1260】涂色paint 区间DP

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1147  Solved: 698[Submit][Sta ...

  7. 【DP】BZOJ 1260: [CQOI2007]涂色paint

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 893  Solved: 540[Submit][Stat ...

  8. BZOJ 1260: [CQOI2007]涂色paint( 区间dp )

    区间dp.. dp( l , r ) 表示让 [ l , r ] 这个区间都变成目标颜色的最少涂色次数. 考虑转移 : l == r 则 dp( l , r ) = 1 ( 显然 ) s[ l ] = ...

  9. BZOJ_1260_[CQOI2007]涂色paint _区间DP

    BZOJ_1260_[CQOI2007]涂色paint _区间DP 题意: 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字 ...

随机推荐

  1. oracle之 sqlplus prelim 参数介绍 ( 处理hang )

    从Oracle10g开始,sqlplus提供了一个参数选项-prelim,用这个参数,在系统已经hang的时候.我们可以连接到SGA而不是数据库,也就是说没有session被创建. 一. 通过以下步骤 ...

  2. java的RTTI和反射机制

    RTTI,即Run-Time Type Identification,运行时类型识别.RTTI能在运行时就能够自动识别每个编译时已知的类型. 很多时候需要进行向上转型,比如Base类派生出Derive ...

  3. Hibernate学习8—Hibernate 映射关系(一对一)

    第一节:Hibernate 一对一映射关系实现 假设一个用户对应一个地址:   1)按照主键映射: User.java: package com.cy.model; public class User ...

  4. appium+python自动化33-解锁九宫格(TouchAction)

    TouchAction 1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.py class TouchActio ...

  5. 阻塞队列之五:LinkedBlockingQueue

    一.LinkedBlockingQueue简介 LinkedBlockingQueue是一个使用链表完成队列操作的阻塞队列.链表是单向链表,而不是双向链表.采用对于的next构成链表的方式来存储对象. ...

  6. java程序中的ibatis连接mySql的基本实例

    属性文件:SqlMap.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ibatis username= ...

  7. Julia - 整数型

    所有的基础数据类型通过灵活用户可扩展的类型提升系统 ,不需显式类型转换,就可以互相运算 整数的默认类型,取决于系统是 32 位还是 64 位 julia> typeof(1) # 32 位系统 ...

  8. windows server 2008 配置DNS服务器与IIS

    0x00: 总结这个星期在学校学的. 0x01安装: 首先你得安装好windows server 2008 然后在添加角色->安装IIS和DNS服务器 勾选好你要安装的. 安装-> 根据老 ...

  9. python 3389爆破机

    前言: = =上学后的第一个星期假期,写了个3389爆破器 - 0x01 准备: hydra 钟馗之眼API 0x02代码: import optparse import os import requ ...

  10. Python小知识点(2)

    1.简述Python的运行过程 在说这个问题之前,我们先来说两个概念,PyCodeObject和pyc文件. 我们在硬盘上看到的pyc自然不必多说,而其实PyCodeObject则是Python编译器 ...