[LintCode] 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.
Notice
All costs are positive integers.
Example
Given n = 3, k = 3, costs = [[14,2,11],[11,14,5],[14,3,10]] return 10
house 0 is color 2, house 1 is color 3, house 2 is color 2, 2 + 5 + 3 = 10
LeetCode上的原题,请参见我之前的博客Paint House II。
class Solution {
public:
/**
* @param costs n x k cost matrix
* @return an integer, the minimum cost to paint all houses
*/
int minCostII(vector<vector<int>>& costs) {
if (costs.empty() || costs[].empty()) return ;
int m = costs.size(), n = costs[].size();
int min1 = , min2 = , idx1 = -;
for (int i = ; i < m; ++i) {
int m1 = INT_MAX, m2 = m1, id1 = -;
for (int j = ; j < n; ++j) {
int cost = costs[i][j] + (j == idx1 ? min2 : min1);
if (cost < m1) {
m2 = m1; m1 = cost; id1 = j;
} else if (cost < m2) {
m2 = cost;
}
}
min1 = m1; idx1 = id1; min2 = m2;
}
return min1;
}
};
[LintCode] Paint House II 粉刷房子之二的更多相关文章
- [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] 265. 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]265. Paint House II粉刷房子(K色可选)
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- [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 ...
- [Swift]LeetCode256.粉刷房子 $ Paint House
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 265. 粉刷房子 II
Q: A: 首先这题可以和粉刷房子这题一样解法,对于i号房子,遍历k种颜色,对于每一种,都去找i-1号房子除该颜色之外的最小花费.但上一题是3种颜色,总复杂度O(N),这题k种颜色,复杂度O(NK^2 ...
随机推荐
- Android的图片缓存ImageCache(转)
为什么要做缓存? 在UI界面加载一张图片时很简单,然而如果需要加载多张较大的图像,事情就会变得更加复杂.在许多情况下(如ListView.GridView或ViewPager等的组件),屏 ...
- 有关servlet初学者的资源和建议
四天来学习servlet是很痛苦的经历,其实可以不必要这么痛苦,关键是一定要学会冷静的分析问题与解决问题,要不言学习也没有那么多的乐趣.初学java刚满15天. 首先对于资源来说建议先读一点点的PPT ...
- 聊聊传统oo和js的某些对比——对象/函数/new关键字等
自己的学习记录,写的短点可以以后短时间内理清一些疑惑,看前要求你至少了解js中关于原型链等基本概念,因为文章直接以总结的形式理出知识点,没有去解释一些基本的概念! 1.1.熟记两句话,预预热 1. 函 ...
- JMeter常用字符串相关函数
JMeter的惯用函数使用-字符串相关 主要的函数如下:1.将字符串转为大写或小写: ${__lowercase(Hello,)} ${__uppercase(Hello,)}2.生成字符串: _ ...
- hdu 1203 概率+01背包
I NEED A OFFER! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- 某app客户端数字签名分析
最近测试app时发现某app对数据包做了签名,其直接后果就导致截获的数据包没法修改,因此对该app的数字签名了进行了一次分析.
- delphi override、overload、reintroduce的区别-0613.txt
http://blog.csdn.net/honglixx/article/details/3624934 1.override overload reintroduce的中文叫法是什么? overr ...
- 递推水题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table
题目传送门 /* 模拟递推水题 */ #include <cstdio> #include <iostream> #include <cmath> #include ...
- ASP.NET Core EF Sample
Install EF Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityF ...
- xampp的Apache无法启动解决方法
XAMPP Apache 无法启动原因1(缺少VC运行库): 这个就是我遇到的问题原因,下载安装的XAMPP版本是xampp-win32-1.7.7-VC9,而现有的Windows XP系统又没有安装 ...