题目地址: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:

  1. Input: [[17,2,17],[16,16,5],[14,3,19]]
  2. Output: 10
  3. Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
  4. 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]。那么递推公式是:

  1. dp[n][0] = min(dp[n - 1][1], dp[n - 1][2]) + costs[n - 1][0];
  2. dp[n][1] = min(dp[n - 1][0], dp[n - 1][2]) + costs[n - 1][1];
  3. dp[n][2] = min(dp[n - 1][0], dp[n - 1][1]) + costs[n - 1][2];

意义是不能和前面的房子刷相同的颜色,那么就使用前面房子另外两种颜色的累计最小花费,加上刷当前的颜色的花费。最后去最小即可。

C++代码如下:

  1. class Solution {
  2. public:
  3. int minCost(vector<vector<int>>& costs) {
  4. const int N = costs.size();
  5. vector<vector<int>> dp(N + 1, vector<int>(3, 0));
  6. int res = 0;
  7. for (int n = 1; n <= N; ++n) {
  8. dp[n][0] = min(dp[n - 1][1], dp[n - 1][2]) + costs[n - 1][0];
  9. dp[n][1] = min(dp[n - 1][0], dp[n - 1][2]) + costs[n - 1][1];
  10. dp[n][2] = min(dp[n - 1][0], dp[n - 1][1]) + costs[n - 1][2];
  11. }
  12. return min(dp[N][0], min(dp[N][1], dp[N][2]));
  13. }
  14. };

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】256. Paint House 解题报告(C++)的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. Nginx编译参数详细介绍

    /configure --help --help 显示本提示信息 --prefix=PATH 设定安装目录 --sbin-path=PATH 设定程序文件目录 --conf-path=PATH 设定配 ...

  2. 云原生PaaS平台通过插件整合SkyWalking,实现APM即插即用

    一. 简介 SkyWalking 是一个开源可观察性平台,用于收集.分析.聚合和可视化来自服务和云原生基础设施的数据.支持分布式追踪.性能指标分析.应用和服务依赖分析等:它是一种现代 APM,专为云原 ...

  3. MapReduce05 框架原理OutPutFormat数据输出

    目录 4.OutputFormat数据输出 OutputFormat接口实现类 自定义OutputFormat 自定义OutputFormat步骤 自定义OutputFormat案例 需求 需求分析 ...

  4. SQLyog连接mysql8报2058错误

    连接会话时,报如下错误. 通过网上查解决办法,报这个错误的原因是mysql密码加密方法变了 解决办法: 1.先使用mysql -uroot -p输入密码进去mysql 2.ALTER USER 'ro ...

  5. day11 序列化组件、批量出入、自定义分页器

    day11 序列化组件.批量出入.自定义分页器 今日内容详细 ajax实现删除二次提醒(普通版本) ajax结合第三方插件sweetalert实现二次提醒(样式好看些) ajax如何发送文件数据 aj ...

  6. Kotlin 学习(2)

    属性和字段 1.声明属性 Kotlin中可以使用var关键字声明可变属性,或者用val关键字声明只读属性,属性的类型在后面,变量名在前面,中间加冒号和空格. public class Address ...

  7. elasticSearch索引库查询的相关方法

    package com.hope.es;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.c ...

  8. 南邮CTF-MISC-Remove Boyfriend

    Remove Boyfriend 打开wireshark,找到关键字部分Remove Boyfriend 在第五行 在此行右击 点击追踪流 选择TCP流,可以分析出流量的传输过程 通过上面的执行列表 ...

  9. Android CameraX ImageAnalysis 获取视频帧

    CameraX使用ImageAnalysis分析器,可以访问缓冲区中的图像,获取视频帧数据. 准备工作 准备工作包括gradle,layout,动态申请相机权限,外部存储权限等等,大部分设置与Came ...

  10. [BUUCTF]PWN——picoctf_2018_rop chain

    picoctf_2018_rop chain 附件 步骤: 例行检查,32位,开启了NX保护 试运行一下程序,看到输入太长数据会崩溃 32位ida载入,习惯性的检索程序里的字符串,看见了flag.tx ...