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 ...
随机推荐
- python:列表与元组
1.python包含六种内建的序列,列表和元组是其中的两种,列表可以修改,元组则不能 2.通用序列操作 2.1 索引:和C#的区别是索引可以为负数,最后一个元素索引为-1,索引超出范围会报错 例:&g ...
- 【Java EE 学习 36】【struts2】【struts2系统验证】【struts2 ognl值栈】【struts2 ongl标签】【struts2 UI标签】【struts2模型驱动和令牌机制】
一.struts2系统验证 1.基于struts2系统验证的方式实际上就是通过配置xml文件的方式达到验证的目的. 2.实际上系统校验的方法和手工校验的方法在底层的基本实现是相同的.但是使用系统校验的 ...
- 【转】算法杂货铺——k均值聚类(K-means)
k均值聚类(K-means) 4.1.摘要 在前面的文章中,介绍了三种常见的分类算法.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别与之对应.但是很多时 ...
- post NSURLConnection请求网络数据
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- Python爬虫学习(5): 简单的爬取
学习了urllib,urlib2以及正则表达式之后就可以做一些简单的抓取以及处理工作.为了抓取方便,这里选择糗事百科的网页作为抓取对象. 1. 获取数据: In [293]: url = " ...
- Gollum 安装笔记
环境Ubuntu server 14.04 sudo apt-get install ruby1.9.1 ruby1.9.1-dev make zlib1g-dev libicu-dev build- ...
- bulk collect no_data_found exception
Bulk collect当没有数据抛出异常跟implicit cursor 处理不一样. 先看一下implicit cursor的处理吧: cl scr; DECLARE l_descr hardwa ...
- Leetcode Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- POJ 3278 题解
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 78114 Accepted: 24667 ...