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.

Example:

Input: n = 3, k = 2
Output: 6
Explanation: Take c1 as color 1, c2 as color 2. All possible ways are:   post1 post2 post3
----- ----- ----- -----
1 c1 c1 c2
  2 c1 c2 c1
  3 c1 c2 c2
  4 c2 c1 c1 
5 c2 c1 c2
  6 c2 c2 c1

这道题让我们粉刷篱笆,有n个部分需要刷,有k种颜色的油漆,规定了不能有超过连续两根柱子是一个颜色,也就意味着第三根柱子要么根第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色,问总共有多少种刷法。那么首先来分析一下,如果 n=0 的话,说明没有需要刷的部分,直接返回0即可,如果n为1的话,那么有几种颜色,就有几种刷法,所以应该返回k,当 n=2 时, k=2 时,可以分两种情况来统计,一种是相邻部分没有相同的,一种相同部分有相同的颜色,那么对于没有相同的,对于第一个格子,有k种填法,对于下一个相邻的格子,由于不能相同,所以只有 k-1 种填法。而有相同部分颜色的刷法和上一个格子的不同颜色刷法相同,因为下一格的颜色和之前那个格子颜色刷成一样的即可,最后总共的刷法就是把不同和相同两个刷法加起来,参见代码如下:

解法一:

class Solution {
public:
int numWays(int n, int k) {
if (n == ) return ;
int same = , diff = k;
for (int i = ; i <= n; ++i) {
int t = diff;
diff = (same + diff) * (k - );
same = t;
}
return same + diff;
}
};

下面这种解法和上面那方法几乎一样,只不过多了一个变量,参见代码如下:

解法二:

class Solution {
public:
int numWays(int n, int k) {
if (n == ) return ;
int same = , diff = k, res = same + diff;
for (int i = ; i <= n; ++i) {
same = diff;
diff = res * (k - );
res = same + diff;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/276

类似题目:

House Robber II

House Robber

Paint House II

Paint House

参考资料:

https://leetcode.com/problems/paint-fence/

https://leetcode.com/problems/paint-fence/discuss/71156/O(n)-time-java-solution-O(1)-space

https://leetcode.com/problems/paint-fence/discuss/178010/The-only-solution-you-need-to-read

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Paint Fence 粉刷篱笆的更多相关文章

  1. [LintCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...

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

  3. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  4. LeetCode Paint Fence

    原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...

  5. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

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

  7. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  8. [Locked] Paint Fence

    Paint Fence There is a fence with n posts, each post can be painted with one of the k colors. You ha ...

  9. [Swift]LeetCode276. 粉刷栅栏 $ Paint Fence

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

随机推荐

  1. 分布式系统理论进阶 - Paxos变种和优化

    引言 <分布式系统理论进阶 - Paxos>中我们了解了Basic Paxos.Multi Paxos的基本原理,但如果想把Paxos应用于工程实践,了解基本原理还不够. 有很多基于Pax ...

  2. 通过三个DEMO学会SignalR的三种实现方式

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...

  3. 深入学习jQuery描述文本内容的3个方法

    × 目录 [1]html() [2]text() [3]val()[4]总结 前面的话 在javascript中,描述元素内容有5个属性,分别是innerHTML.outerHTML.innerTex ...

  4. Linux零起点之进程管理----c语言编程

    进程 (Process)是指操作系统中被加载到内存中的.正在运行的应用程序实例.进程是系统资源分配的基本单元,在其生命周期内会使用系统中的各种资源.进程主要由程序.数据以及进程控制快(PCB)3个部分 ...

  5. Devexress XPO xpPageSelector 使用

    在官网找到的.在这里做个备注. private void simpleButton1_Click(object sender, EventArgs e) { ) return; xpPageSelec ...

  6. JavaWeb_day02_登录校验_查询所有员工信息_DeBug

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! WEB_day02 servlet 协议转对象 服务器 ...

  7. Android 的进程和线程

    进程和线程 如果某个应用程序组件是第一次被启动,且这时应用程序也没有其他组件在运行,则android系统会为应用程序创建一个包含单个线程的linux进程.默认情况下,同一个应用程序的所有组件都运行在同 ...

  8. SHA-256算法

    SHA-.h #ifndef _SHA_256_H #define _SHA_256_H #include<iostream> using namespace std; typedef u ...

  9. 严重: Exception sending context initialized event to listener instance of class

    问题描述:Exception sending context initialized event to listener instance of class org.springframework.w ...

  10. java类与实例

    最近在看设计模式,感觉自己对java的三大特性的理解不够清晰,搞不清楚抽象类.接口.泛型的用处和优缺点.设计模式学了一半,想着还是停下来脑补一下java的基础,就从java对象开始吧. 一.java对 ...