【LeetCode】276. Paint Fence 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/sum-of-digits-in-the-minimum-number/
题目描述
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
题目大意
有 k 种颜色的涂料和一个包含 n 个栅栏柱的栅栏,每个栅栏柱可以用其中一种颜色进行上色。
你需要给所有栅栏柱上色,并且保证其中相邻的栅栏柱最多连续两个颜色相同。然后,返回所有有效涂色的方案数。
解题方法
动态规划
设F[n]为到第n个栅栏总的配色方案数,设当前的栅栏为cur,前面一个栅栏为prev,更前面的一个栅栏为pprev.
当前栅栏cur的涂色方案有两种:
- 和prev相同,此时说明prev的颜色应与pprev的颜色不同,pprev的涂色方法有 F(n - 2) 种,prev的涂色方式有 (k - 1) 种,所以此时情况应为 F(n - 2) * (k - 1)
- 和prev不同,prev的涂色方法有 F(n - 1) 种,cur的涂色方式有 (k - 1) 种,此时情况应为 F(n - 1) * (k - 1)
所以递推公式应为 F(n) = F(n - 2) * (k - 1) + F(n - 1) * (k - 1)
C++代码如下:
class Solution {
public:
int numWays(int n, int k) {
if (n == 0) return 0;
if (n == 1) return k;
int pprev = k;
int prev = k * k;
int cur = k * k;
for (int i = 2; i < n; ++i) {
cur = pprev * (k - 1) + prev * (k - 1);
pprev = prev;
prev = cur;
}
return cur;
}
};
参考资料:https://leetcode-cn.com/problems/paint-fence/solution/c-dp-zong-jie-yi-xia-liang-chong-si-lu-by-sheng-be/
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】276. Paint Fence 解题报告(C++)的更多相关文章
- [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
Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...
- 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 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- 【豆科基因组】大豆适应性位点GWAS分析 [转载]
目录 材料与方法 结果分析 本文利用99085个高质量SNP 通过STRUCTURE,PCA和neighbour-joining tree的群体结构分析将地方品种分为三个亚群,这些亚群表现出地理上的遗 ...
- 【百奥云GS专栏】1-全基因组选择介绍
目录 什么是基因组选择? 基因组选择技术的发展 基因组选择的原理和流程 基因组选择的模型 基因组选择的展望 参考资料 什么是基因组选择? 基因组选择(Genomic Selection,简称GS)这一 ...
- 认识Influxdb时序数据库及Influxdb基础命令操作
认识Influxdb时序数据库及Influxdb基础命令操作 一.什么是Influxdb,什么又是时序数据库 Influxdb是一个用于存储时间序列,事件和指标的开源数据库,由Go语言编写而成,无需外 ...
- 23-Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- PL\SQL和PL/SQL Developer 12安装与配置
安装: (1)在已有安装包的情况下,直接将安装包解压到文件夹下,注意不要解压缩到c:\programs Files(x86)的文件夹下,不能解压缩到有中文文件夹命名的文件夹下面 (2)没有安装包的情况 ...
- 搭建简单的SpringCloud项目一:注册中心和公共层
注:笔者在搭建途中其实遇见不少问题,统一放在后面的文章说明,现在的搭建是测试OK的. GitHub:https://github.com/ownzyuan/test-cloud 后续:搭建简单的Spr ...
- 巩固javawbe第二天
巩固内容: <!DOCTYPE> 声明 <!DOCTYPE>声明有助于浏览器中正确显示网页. 网络上有很多不同的文件,如果能够正确声明HTML的版本,浏览器就能正确显示网页内容 ...
- window ntp服务
一.确定服务端和客户端处于同一网段,能相互 Ping 通. 二.服务器端(Server)配置 1.选择一台服务器(Windows 系统)作为时间同步服务器: 2.Win + R (运行 cmd 命令行 ...
- 【分布式】ZooKeeper权限控制之ACL(Access Control List)访问控制列表
zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...
- js处理title超长问题
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...