[LeetCode] Paint House I & II
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()[]));
}
};
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的更多相关文章
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- [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 ...
- LeetCode Paint House II
原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...
- [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 ...
- [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 ...
随机推荐
- 关于IOS某图片添加控件,图片从相册或拍照保存后,再次进入时点击放大图无法显示的问题
某图片添加控件: https://github.com/XZTLLQ/LQPhotoPickerDemo 问题: 标题已说明 代码块: NSArray *alAssetUrl =(NSMutableA ...
- Linux中wget用法
Wget简介:Linux系统中wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTP ...
- DOM,浏览器,javascript,html之间的关系
来源于:https://github.com/hucheng91/myBlog/blob/master/web/dom/dom.md DOM定义 DOM可以以一种独立于平台和语言的方式访问和修改一个文 ...
- [转载]Error starting Sun's native2ascii:
原文地址:Error starting Sun's native2ascii:作者:大一吧浪 Error starting Sun's native2ascii: at org.apache.tool ...
- rox + openbox + fbpanel + conky打造又快又稳的桌面
从开始用 Gentoo 以来,就没有打算用 gnome . KDE 这些巨无霸级别的 DE ,最后选择了相对来来说比较轻量级的 Xfce4 ,不过最近更是变本加厉,连 Xfce4 都觉得太大.于是,下 ...
- JavaScript 浏览器对象模型 (BOM)
浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器“对话”. 浏览器对象模型 (BOM) 浏览器对象模型(Browser Object Model)尚无正式标准. 由于现代浏览器已经 ...
- 进阶之路(中级篇) - 017 有关于Arduino 驱动舵机及相关问题
/********************************* 代码功能:通过串口控制电机 使用函数: Serial.available(); //判断串口是否接收到数据 Serial.prin ...
- 树莓派进阶之路 (008) - 树莓派安装ftp服务器(转)
vsftpd是开源的轻量级的常用ftp服务器. 1,安装vsftpd服务器 (约400KB) sudo apt-get install vsftpd 2,启动ftp服务 sudo serv ...
- C++ map,set内部数据结构
1)Set是一种关联容器,它用于存储数据,并且能从一个数据集合中取出数据.它的每个元素的值必须唯一,而且系统会根据该值来自动将数据排序.每个元素的值不能直接被改变.[重点]内部结构采用红黑树的平衡二叉 ...
- HDU 3820 Golden Eggs (SAP | Dinic)
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...