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.

 class Solution {
public:
int minCost(vector<vector<int>>& costs) {
if (costs.empty()) return ;
for (int i = ; i < costs.size(); ++i) {
for (int j = ; j < ; ++j) {
costs[i][j] += min(costs[i-][(j+)%], costs[i-][(j+)%]);
}
}
return min(costs.back()[], min(costs.back()[], costs.back()[]));
}
};

Paint House II

There are a row of n houses, each house can be painted with one of the k colors. 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 k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2]is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Follow up:
Could you solve it in O(nk) runtime?

 class Solution {
public:
int minCostII(vector<vector<int>>& costs) {
if (costs.empty() || costs[].empty()) return ;
int n = costs.size(), k = costs[].size();
vector<int> min1(k), min2(k);
for (int i = ; i < costs.size(); ++i) {
min1[] = INT_MAX;
for (int j = ; j < k; ++j) {
min1[j] = min(min1[j-], costs[i-][j-]);
}
min2[k-] = INT_MAX;
for (int j = k - ; j >= ; --j) {
min2[j] = min(min2[j+], costs[i-][j+]);
}
for (int j = ; j < k; ++j) {
costs[i][j] += min(min1[j], min2[j]);
}
}
int res = INT_MAX;
for (auto c : costs.back()) {
res = min(res, c);
}
return res;
}
};

快速找到数组中去掉某个元素的最小值方法:定义两个数组,min1[i]与min2[i]分别记录从左向右到第i位与从右向左到第i位的区间最小值,那么去掉第i位的最小值就是min(min1[i], min2[i])。

[LeetCode] Paint House I & II的更多相关文章

  1. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  4. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  5. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  6. [LeetCode] 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 ...

  7. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

  8. [LeetCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  9. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

随机推荐

  1. Maven的坐标与资源库

    在Maven世界中,每个工程都有它唯一的 组织名.模块名.版本 ,这三个就是maven项目的坐标,一个maven工程可以打包成jar.war.pom等形式,但是它们都是拥有上述三个坐标的.我们在项目过 ...

  2. Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node ……

    解决办法是加一个等待时间即可解决问题: setTimeout(function () { you code }, );

  3. 关于继承中的super()调用父类构造方法

    super super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类. 每当你new一个子类对象的时候,子类对象都隐式内置了一个父类对象.所以说,为了那个内置的父类 ...

  4. 【转】IP地址、子网掩码、网络号、主机号、网络地址、主机地址以及ip段

    背景知识 IP地址 IP地址被用来当做Internet上的电脑的身份编号.大家日常见到的情况是每台联网的PC上都需要有IP地址,才能正常通信.我们可以把“个人电脑”比作“一台电话”,那么“IP地址”就 ...

  5. Ubuntu 10.04里安装强大抓图工具Shutter

    Shutter 原名叫GScrot,是一款Linux下很强大的抓图工具,支持截取全屏幕,窗口,自选区域以及网页的图像,并且截取后的图像还可以简单编辑,Shutter还支持插件功能,矩齿,边缘化,碎纸, ...

  6. [转]GAN论文集

    really-awesome-gan A list of papers and other resources on General Adversarial (Neural) Networks. Th ...

  7. javascript 基础知识学习1

    JavaScript 是脚本语言.浏览器会在读取代码时,逐行地执行脚本代码.而对于传统编程来说,会在执行前对所有代码进行编译.基础知识:1).JavaScript 对大小写敏感.JavaScript ...

  8. 我的学习工作经历,一个园林专业中专毕业生的IT之路 学习编程 创业

    我的学习工作经历,一个园林专业中专毕业生的IT之路 魏琼东,男,1983年生人,祖籍甘肃陇南人,首先得感谢我父亲给我取了这么一个好名字,至少我非常喜欢他,因为目前还没有发现和我同名的人. 我是1998 ...

  9. Using 1-Wire device with Intel Galileo

    Using 1-Wire device with Intel Galileo 3 Replies Many people have had trouble with getting 1-Wire de ...

  10. SQL数据缓存依赖总结

    以前只听过SQL server数据缓存依赖,但一直没使用,由于项目需要,才研究了一番,发现了一个很诡异的问题,竟然是一个操作顺序问题导致的. SQL server数据缓存依赖有两种实现模式,轮询模式, ...