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 粉刷房子之二的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  5. [LintCode] Sort Integers II 整数排序之二

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

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

  7. [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 ...

  8. 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:二叉树下的不能相邻,求能 ...

  9. 265. 粉刷房子 II

    Q: A: 首先这题可以和粉刷房子这题一样解法,对于i号房子,遍历k种颜色,对于每一种,都去找i-1号房子除该颜色之外的最小花费.但上一题是3种颜色,总复杂度O(N),这题k种颜色,复杂度O(NK^2 ...

随机推荐

  1. 去除字符串中的html标记及标记中的内容

    去除字符串中的html标记及标记中的内容 --1.创建函数 create function [dbo].[clearhtml] (@maco varchar(8000)) returns varcha ...

  2. 【微信Java开发 --1】内网穿透外网,使用外网域名可以访问到本地项目

    1.首先上https://natapp.cn/ 2.接下来在网站申请账号 3.购买免费隧道 4.为你的免费隧道设置名称以及端口号,由于本人本地的使用Tomcat做服务器,所以用惯了8080端口,因此设 ...

  3. 安装Maven、Eclipse设置、添加地址JAR

    1.下载Maven 地址:http://maven.apache.org/download.cgi 2.安装Maven 系统变量:MAVEN_HOME = D:\maven\apache-maven- ...

  4. Android如何缩减APK包大小

    代码 保持良好的编程习惯,不要重复或者不用的代码,谨慎添加libs,移除使用不到的libs. 使用proguard混淆代码,它会对不用的代码做优化,并且混淆后也能够减少安装包的大小. native c ...

  5. iOS10 UI教程基改变视图的外观与视图的可见性

    iOS10 UI教程基改变视图的外观与视图的可见性 视图是应用程序的界面,是用户在屏幕上看到的对象.用户可以通过触摸视图上的对象与应用程序进行交互,所以视图界面的优劣会直接影响到了客户体验的好坏.和视 ...

  6. xor方程组消元 UVA 11542 Square

    题目传送门 题意:给n个数,选择一些数字乘积为平方数的选择方案数.训练指南题目. 分析:每一个数字分解质因数.比如4, 6, 10, 15,, , , , 令,表示选择第i个数字,那么,如果p是平方数 ...

  7. MapReduce 作业调试

    1. 最经典的方法通过打印语句来调试程序 System.err.println("Bad Data"+value.toString()); 这些输出错误都会记录到一个标准错误中,可 ...

  8. [转]HTML5本地存储——Web SQL Database

    在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数据就 ...

  9. Intellij IDEA tomcat相关

    可能遇到的问题: tomcat7启动报错  java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter h ...

  10. js 四舍五入保留二位小数

    1. 最笨的办法....... [我就怎么干的.........] function get() { var s = 22.127456 + ""; var str = s.sub ...