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. jsp,图片显示

    问题:jsp中显示项目中image文件夹中的图片 1,项目中image文件夹中有对应的图片 2,<img ,src="/项目名/image/图片名.jpg">,用其他变 ...

  2. NuGet学习笔记(3) 搭建属于自己的NuGet服务器

    文章导读 创建NuGetServer Web站点 发布站点到IIS 添加本地站点到包包数据源 在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重 ...

  3. Android开发工具之Dash

    作为一名死coder,每天最常见的动作就是查看各种API文档,你一定也有过同时打开N个窗口(HTML.PDF.CHM),不停的在编辑器与文档之间切换的感受吧?怎么说呢,其实我很讨厌这种枯燥无味的动作, ...

  4. MFC中afx_msg是什么,afx_msg void function()是什么意思

    应用程序框架产生的消息映射函数例如:afx_msg void OnBnClickedButton1(); 其中 afx_msg为消息标志,它向系统声明:有消息映射到函数实现体:而在map宏定义中,就有 ...

  5. Jmeter 小攻略(转)

    http://www.myexception.cn/open-source/1346307.html

  6. select 框option添加属性 js计算价格 保持两位小数

    <select name="" id=""> <volist name="v['childList']" id=" ...

  7. host_network_interfaces_slow_mode_thresholds

    CDH报集群异常: host_network_interfaces_slow_mode_thresholds 警告置为从不-- 主要是我没有查到发生这种情况的原因是什么--

  8. 计数排序-java

    今天看了一本书,书里有道题,题目很常见,排序,明了点说: 需求:输入:最多有n个正整数,每个数都小于n, n为107 ,没有重复的整数 输出:按升序排列 思路:假设有一组集合 {1,3,5,6,11, ...

  9. JavaScript设计模式——单体模式

    一:单体模式简介: 是什么:将代码组织为一个逻辑单元,这个单元中的代码通过单一的变量进行访问.只要单体对象存在一份实例,就可以确信自己的所有代码使用的是同样的全局资源. 用途:1.用来划分命名空间,减 ...

  10. 字体和壁纸合并后再更改壁纸--《用delphi开发共享软件》-15.2桌面提示器

    procedure TFrmPlay.mnDeskPicClick(Sender: TObject); Var s:String; i:Integer; begin //s:=Path+'SetPic ...