【LeetCode】256. Paint House 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/paint-house/
题目描述
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green
. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x 3
cost matrix. For example, costs[0][0]
is the cost of painting house 0 with color red
; costs[1][2]
is the cost of painting house 1 with color green
, and so on… Find the minimum cost to paint all houses.
Note:
All costs are positive integers.
Example:
Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
Minimum cost: 2 + 5 + 3 = 10.
题目大意
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个n x 3的矩阵来表示的。
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2]表示第 1 号房子粉刷成绿色的花费,以此类推。请你计算出粉刷完所有房子最少的花费成本。
解题方法
动态规划
计算刷完第n房子为止,三种颜色分别累计需要的最少花费dp[n][0], dp[n][1], d[n][2]。那么递推公式是:
dp[n][0] = min(dp[n - 1][1], dp[n - 1][2]) + costs[n - 1][0];
dp[n][1] = min(dp[n - 1][0], dp[n - 1][2]) + costs[n - 1][1];
dp[n][2] = min(dp[n - 1][0], dp[n - 1][1]) + costs[n - 1][2];
意义是不能和前面的房子刷相同的颜色,那么就使用前面房子另外两种颜色的累计最小花费,加上刷当前的颜色的花费。最后去最小即可。
C++代码如下:
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
const int N = costs.size();
vector<vector<int>> dp(N + 1, vector<int>(3, 0));
int res = 0;
for (int n = 1; n <= N; ++n) {
dp[n][0] = min(dp[n - 1][1], dp[n - 1][2]) + costs[n - 1][0];
dp[n][1] = min(dp[n - 1][0], dp[n - 1][2]) + costs[n - 1][1];
dp[n][2] = min(dp[n - 1][0], dp[n - 1][1]) + costs[n - 1][2];
}
return min(dp[N][0], min(dp[N][1], dp[N][2]));
}
};
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】256. Paint House 解题报告(C++)的更多相关文章
- 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 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- Nginx编译参数详细介绍
/configure --help --help 显示本提示信息 --prefix=PATH 设定安装目录 --sbin-path=PATH 设定程序文件目录 --conf-path=PATH 设定配 ...
- 云原生PaaS平台通过插件整合SkyWalking,实现APM即插即用
一. 简介 SkyWalking 是一个开源可观察性平台,用于收集.分析.聚合和可视化来自服务和云原生基础设施的数据.支持分布式追踪.性能指标分析.应用和服务依赖分析等:它是一种现代 APM,专为云原 ...
- MapReduce05 框架原理OutPutFormat数据输出
目录 4.OutputFormat数据输出 OutputFormat接口实现类 自定义OutputFormat 自定义OutputFormat步骤 自定义OutputFormat案例 需求 需求分析 ...
- SQLyog连接mysql8报2058错误
连接会话时,报如下错误. 通过网上查解决办法,报这个错误的原因是mysql密码加密方法变了 解决办法: 1.先使用mysql -uroot -p输入密码进去mysql 2.ALTER USER 'ro ...
- day11 序列化组件、批量出入、自定义分页器
day11 序列化组件.批量出入.自定义分页器 今日内容详细 ajax实现删除二次提醒(普通版本) ajax结合第三方插件sweetalert实现二次提醒(样式好看些) ajax如何发送文件数据 aj ...
- Kotlin 学习(2)
属性和字段 1.声明属性 Kotlin中可以使用var关键字声明可变属性,或者用val关键字声明只读属性,属性的类型在后面,变量名在前面,中间加冒号和空格. public class Address ...
- elasticSearch索引库查询的相关方法
package com.hope.es;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.c ...
- 南邮CTF-MISC-Remove Boyfriend
Remove Boyfriend 打开wireshark,找到关键字部分Remove Boyfriend 在第五行 在此行右击 点击追踪流 选择TCP流,可以分析出流量的传输过程 通过上面的执行列表 ...
- Android CameraX ImageAnalysis 获取视频帧
CameraX使用ImageAnalysis分析器,可以访问缓冲区中的图像,获取视频帧数据. 准备工作 准备工作包括gradle,layout,动态申请相机权限,外部存储权限等等,大部分设置与Came ...
- [BUUCTF]PWN——picoctf_2018_rop chain
picoctf_2018_rop chain 附件 步骤: 例行检查,32位,开启了NX保护 试运行一下程序,看到输入太长数据会崩溃 32位ida载入,习惯性的检索程序里的字符串,看见了flag.tx ...