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.

Notice

All costs are positive integers.

Example
Given costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10

LeetCode上的原题,请参见我之前的博客Paint House

解法一:

class Solution {
public:
/**
* @param costs n x 3 cost matrix
* @return an integer, the minimum cost to paint all houses
*/
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[].empty()) return ;
vector<vector<int>> dp = costs;
for (int i = ; i < dp.size(); ++i) {
for (int j = ; j < ; ++j) {
dp[i][j] += min(dp[i - ][(j + ) % ], dp[i - ][(j + ) % ]);
}
}
return min(dp.back()[], min(dp.back()[], dp.back()[]));
}
};

解法二:

class Solution {
public:
/**
* @param costs n x 3 cost matrix
* @return an integer, the minimum cost to paint all houses
*/
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[].empty()) return ;
vector<vector<int>> dp = costs;
for (int i = ; i < dp.size(); ++i) {
dp[i][] += min(dp[i - ][], dp[i - ][]);
dp[i][] += min(dp[i - ][], dp[i - ][]);
dp[i][] += min(dp[i - ][], dp[i - ][]);
}
return min(dp.back()[], min(dp.back()[], dp.back()[]));
}
};

[LintCode] Paint House 粉刷房子的更多相关文章

  1. [leetcode]256. Paint House粉刷房子(三色可选)

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

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

  3. [LeetCode] 256. Paint House 粉刷房子

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

  4. [LintCode] Paint Fence 粉刷篱笆

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

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

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

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

  8. 265. 粉刷房子 II

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

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

随机推荐

  1. [转]svn常用命令

    谢谢原作者:http://blog.sina.com.cn/s/blog_963453200101eiuq.html 1.检出svn  co  http://路径(目录或文件的全路径) [本地目录全路 ...

  2. jquery中append()、prepend()、after()、before()的区别详解

    append() - 在被选元素的结尾插入内容(内容的结尾,比如说有个a标签,则是在</a>这个标签之前添加东西) prepend() - 在被选元素的开头插入内容(内容的开始,比如说有个 ...

  3. winform基础窗体设置及基础控件

    WinForm - 也叫做C/S  客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...

  4. Java api

    StringBuilder.charAt(int index); StringBuilder.deleteCharAt(int index); StringBuilder.setCharAt(int ...

  5. Liferay 6.2 改造系列之二十:修改默认添加用户权限及用户是否拥有个人站点的配置

    1.修改默认添加用户权限为User,而非Powers User: 在/portal-master/portal-impl/src/portal.properties文件中有如下配置: # # Inpu ...

  6. DSP using MATLAB 示例 Example3.11

    用到的性质 上代码: n = -5:10; x = rand(1,length(n)); k = -100:100; w = (pi/100)*k; % freqency between -pi an ...

  7. js-DOM,DOM扩展

    DOM: 1. 了解节点的信息:nodeName(属性的标签名),nodeValue两个属性 在取得信息之前要进行判断是不是节点,节点类型由12个数值常量进行表示 2.每个节点都有一个childNod ...

  8. 关于最近Google无法正常访问的变通之法(已经被墙)

    对于习惯使用谷歌搜素的骚年最近可能比较蛋疼,没这方面需求的就另当别论.可能是政府限制加强的原因,有时访问谷歌在香港服务器也不大好使,不过最近发现,不通过谷歌域名而直接使用谷歌的ip来访问谷歌响应速度可 ...

  9. 1、Delphi 打开目录和txt文件模块

    //1.打开目录和打开txt文件 procedure TMainForm.bbtnOpenLoClick(Sender: TObject); var sLogName: string; begin s ...

  10. 【面经】用递归方法对二叉树进行层次遍历 && 二叉树深度

    void PrintNodeAtLevel(BiTree T,int level) { // 空树或层级不合理 ) return; == level) { cout << T->da ...