[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 ...
随机推荐
- 出来ios顶部导航掉下来问题
<script type="text/javascript"> setposition(); function setposition(){ var ua = navi ...
- 封装JavaScript的AJAX
// 创建request对象 function createXMLHttpRequest() { try { return new XMLHttpRequest();//大多数浏览器 } catch ...
- 纯window下VMware 安装 OS X El Capitan 原版映像【未完待续】
一.所需软件1.下载OS X El Capitan 10.11.2 15C50链接:http://pan.baidu.com/s/1skuLgAx 密码:u2jf 2.下载VMware Worksta ...
- loadrunner通过C语言实现字符的替换(只能替换单个字符,慎用)
如果按照普通的定义字符串就会出现以下错误: 解决方法如下: 将双引号改成单引号: lr_searchReplace(abc,"test",' ','+'); Action也可以这些 ...
- Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
使用命令关闭占用80端口的程序 sudo fuser -k 80/tcp
- Oracle常用命令
查看数据库锁的情况 SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$ ...
- STL UVA 11991 Easy Problem from Rujia Liu?
题目传送门 题意:训练指南P187 分析:用vector存id下标,可以用map,也可以离散化用数组存(发现不用离散化也可以) map #include <bits/stdc++.h> u ...
- 【SAP BO】【WEBI】【转】Webi实现动态选择度量
我们都知道Web Intelligence具有高级的分析功能,是一个非常灵活的报表工具.在这篇文章里,我会演示一个使用Webi实现动态选择度量对象的方案.首先解释一下什么是”动态选择度量”:例如我们有 ...
- iOS学习02C语言分支结构
1. BOOL类型 返回值:真:YES 假:NO BOOL数据类型占一个字节的内存空间,占位符为%d. 计算机在识别时,YES就替换成1,NO就替换成0. bool是C语言中的布尔类型,返回值为tr ...
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...