LeetCode Paint House II
原题链接在这里:https://leetcode.com/problems/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.
Note:
All costs are positive integers.
题解:
类似Paint House. k中不同的颜色. paint当前house时, 若是当前的颜色k与之前最小cost的颜色不同时,加上之前的最小值. 若是当前k与之前相同就选之前给出第二小cost的颜色.
Time Complexity: O(kn). Space: O(1).
AC Java:
public class Solution {
public int minCostII(int[][] costs) {
if(costs == null || costs.length == 0){
return 0;
}
int min1 = 0; //当前这个house涂完最小的cost
int min2 = 0; //当前这个house涂完第二小的cost
int lastColor = -1; //上个house选择的颜色
for(int i = 0; i<costs.length; i++){
int curMin1 = Integer.MAX_VALUE; //选择到当前颜色时的最小cost
int curMin2 = Integer.MAX_VALUE; //选择到当前颜色时的第二小cost
int curColor = -1;
for(int k = 0; k<costs[i].length; k++){
int newCost = costs[i][k] + (k == lastColor ? min2 : min1);
if(newCost < curMin1){
curMin2 = curMin1;
curMin1 = newCost;
curColor = k;
}else if(newCost < curMin2){
curMin2 = newCost;
}
}
min1 = curMin1;
min2 = curMin2;
lastColor = curColor;
}
return min1;
}
}
LeetCode 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] Paint House I & II
Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...
- 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:二叉树下的不能相邻,求能 ...
- [LeetCode] Paint Fence 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [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 ...
- LeetCode Paint House
原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...
- [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 ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
随机推荐
- MongoDB索引创建(5)
索引创建 1:索引提高查询速度,降低写入速度,权衡常用的查询字段,不必在太多列上建索引 2. 在mongodb中,索引可以按字段升序/降序来创建,便于排序 3. 默认是用btree来组织索引文件,2. ...
- Jquery Mobile开发以及Js对象动态绑定
动态创建对象并绑定属性: var instantiate = function (Type, args) { var Constructor = function () { }; Constructo ...
- ThinkPHP的D方法和M方法的区别
M方法和D方法的区别 ThinkPHP 中M方法和D方法都用于实例化一个模型类,M方法 用于高效实例化一个基础模型类,而 D方法 用于实例化一个用户定义模型类. 使用M方法 如果是如下情况,请考虑使用 ...
- PHP 数据库操作类:ezSQL
EZSQL类介绍: 下载地址:http://www.jb51.net/codes/26393.htmlezsql是一个小型的快速的数据库操作类,可以让你很容易地用PHP操作各种数据库( MySQL.o ...
- Django+Tastypie作后端,RequireJS+Backbone作前端的TodoMVC
一.配置好环境 接着前一篇的例子,顺带测试一下已下载下来example里面的backbone_require的例子 注意:直接本地用backbone.localStorage插件运行TodoMVC会报 ...
- CocoaPods安装和使用
CocoaPods是iOS最常用的第三方类库管理工具,绝大部分有名的开源类库都支持CocoaPods. CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境.幸运的是OS X系统默 ...
- python 自带的ide 不能保存文件
初学python 用shell写的代码结果不能保存,经查询,原因有人说是因为文件里有中文, import random secret =random.randint(1,100) guess=0 tr ...
- ZooKeeper个人笔记之节点的监听
create public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) th ...
- Codeforces Round #107 (Div. 2)
D题 并查集+组合 #include <iostream> #include <cstdio> #include <cstring> using namespace ...
- Android -- 自定义控件(ImageButton)
1. 效果图