题目地址: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++)的更多相关文章

  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. Python os模块与sys模块

    1.os模块简单使用及说明 # -*- coding:utf-8 -*- """ os模块主要用于系统,处理程序与系统交互问题 大部分如属性等功能在linux系统中会使用 ...

  2. Redis的六种特性 场景

    Redis的六种特性 l Strings l Hashs l Lists l Sets l Sorted Sets l Pub/Sub Redis各特性的应用场景 Strings Strings 数据 ...

  3. 虚拟节点轻松应对 LOL S11 百万并发流量——腾竞体育的弹性容器实践

    作者 刘如梦,腾竞体育研发工程师,擅长高并发.微服务治理.DevOps,主要负责电竞服务平台架构设计和基础设施建设. 詹雪娇,腾讯云弹性容器服务EKS产品经理,主要负责 EKS 虚拟节点.容器实例相关 ...

  4. 『学了就忘』Linux文件系统管理 — 65、LVM逻辑卷管理介绍

    目录 1.LVM逻辑卷管理的简介 2.LVM逻辑卷管理的原理 3.总结建立LVM分区的步骤 1.LVM逻辑卷管理的简介 LVM是Logical Volume Manager的简称,中文就是逻辑卷管理. ...

  5. 学习java 7.14

    学习内容: 标准输入输出流 输出语言的本质:是一个标准的输出流 字节打印流 字符打印流 对象序列化流 明天内容: 进程和线程 遇到问题: 用对象序列化流序列化一个对象后,假如我们修改了对象所属的类文件 ...

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

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

  7. 爬虫系列:存储 CSV 文件

    上一期:爬虫系列:存储媒体文件,讲解了如果通过爬虫下载媒体文件,以及下载媒体文件相关代码讲解. 本期将讲解如果将数据保存到 CSV 文件. 逗号分隔值(Comma-Separated Values,C ...

  8. 分配器——allocators

    任何容器的构建都离不开分配器,分配器顾名思义就是分割配置内存资源的组件,分配器的效率直接影响力容器的效率. operator new()和malloc() C/C++底层都是通过malloc()调用系 ...

  9. proguard 混淆工具的用法 (适用于初学者参考)

    一. ProGuard简介 附:proGuard官网 因为Java代码是非常容易反编码的,况且Android开发的应用程序是用Java代码写的,为了很好的保护Java源代码,我们需要对编译好后的cla ...

  10. Vue.js 学习

    一,Vue.js 介绍 Vue 是一套用于构建用户界面的渐进式javascript框架,与其它大型框架不同的是:Vue被设计为可以自底向上逐层应用.Vue的核心库只关注视图层,不仅易于上手,还便于与第 ...