[LeetCode] Paint Fence 粉刷篱笆

时间:2022-09-25 11:32:52

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. &lbrack;LintCode&rsqb; 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. &lbrack;LeetCode&rsqb; 276&period; 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. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb; 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&period; House Robber 、 213&period; House Robber II 、337&period; House Robber III 、256&period; Paint House&lpar;lintcode 515&rpar; 、265&period; Paint House II&lpar;lintcode 516&rpar; 、276&period; Paint Fence&lpar;lintcode 514&rpar;

    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. &lbrack;Locked&rsqb; 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. &lbrack;Swift&rsqb;LeetCode276&period; 粉刷栅栏 &dollar; 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. 基于Deep Learning 的视频识别方法概览

    深度学习在最近十来年特别火,几乎是带动AI浪潮的最大贡献者.互联网视频在最近几年也特别火,短视频.视频直播等各种新型UGC模式牢牢抓住了用户的消费心里,成为互联网吸金的又一利器.当这两个火碰在一起,会 ...

  2. &lbrack;Network&rsqb; HTML、XML和JSON学习汇总

    写在前面:楼主也是刚刚接触这方面的知识,之前完全是零基础,后来经朋友推荐了几个不错的博文,看完以后豁然开朗.但是此博文更加偏重于基础知识介绍(其实更深的楼主也还不了解,这方面的大神请绕道),只是分享个 ...

  3. &lbrack;LeetCode&rsqb; Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. 使用mysql的长连接

    有个资料看得我云里雾里的.现在用自己的言语来总结一下,写文字,能够加深自己的理解.也会在写的过程中帮助自己发现理解方面瑕疵,继续查资料求证. 短链接的缺点:创建一个连接,程序执行完毕后,就会自动断掉与 ...

  5. 开涛spring3&lpar;8&period;1&rpar; - 对ORM的支持 之 8&period;1 概述

    8.1  概述 8.1.1  ORM框架 ORM全称对象关系映射(Object/Relation Mapping),指将Java对象状态自动映射到关系数据库中的数据上,从而提供透明化的持久化支持,即把 ...

  6. 对于错误&OpenCurlyDoubleQuote;Refused to execute script from &&num;39&semi;&period;&period;&period;&&num;39&semi; because its MIME type &lpar;&&num;39&semi;&&num;39&semi;&rpar; is not executable&comma; and strict MIME type checking is enabled&period;”的处理。

    今天在是用公司的报表插件Stimulsoft时发现的问题.之前可以正常使用,突然不能加载了.查看发现得到这个错误. 查看请求头 可以看到,请求正常响应,但是发现 Content-Type是空的,但是引 ...

  7. 定时任务 Cron表达式

    Cron表达式由6~7项组成,中间用空格分开.从左到右依次是: 秒.分.时.日.月.周几.年(可省略) Cron表达式的值可以是数字,也可以是以下符号: "*":所有值都匹配 &q ...

  8. 1226 快速幂 取余运算 洛谷luogu

    还记得 前段时间学习二进制快速幂有多崩溃 当然这次方法略有不同 居然轻轻松松的 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整 ...

  9. 【Jmeter自学】badboy使用(三)

    ==================================================================================================== ...

  10. POJ 2676 Sudoku &lpar;数独 DFS&rpar;

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...