原题链接在这里: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的更多相关文章

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

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

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

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

  6. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

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

  8. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

随机推荐

  1. hdfs的读写数据流

    hdfs的读:      首先客户端通过调用fileSystem对象中的open()函数读取他需要的的数据,fileSystem是DistributedFileSystem的一个实例, Distrib ...

  2. GitHub使用心得

    PHP今天算是学习结束了;版本控制也了解很久了,比较熟悉的是svn,Git一直都不太熟,也可能是它是英文的缘故,感觉很费劲,所以用的不多,但不得不说,它功能真的很强大;今天试着和朋友用Git开发一个简 ...

  3. 【Java EE 学习 32 下】【JQuery】【JQuey中的DOM操作】

    一.JQuery中的DOM操作. 什么是DOM:DOM是一中和浏览器.平台.语言无关的接口,使用该接口可以轻松访问页面中所有的标准组件.DOM简称文档对象模型,是Document Oject Mode ...

  4. 享元模式/Flyweight模式/对象结构型/设计模式

    flyweight 享元模式(对象结构型) Flyweight在拳击比赛中指最轻量级,即"蝇量级"或"雨量级",这里选择使用"享元模式"的意 ...

  5. Log4j框架

    一.Log4j基本使用方法 Log4j由三个重要的组件构成:日志信息的优先级 Loggers,日志信息的输出目的地 Appenders,日志信息的输出格式 Layouts .日志信息的优先级从高到低 ...

  6. Listview的使用

    最近一个多月忙着使用新的技术来做项目,现在项目上线了,嗯,发现android有些生疏了,所以今天特地写了这一篇博客来相信的讲解一些基础知识,同事呢,也可以让我温故知新一下.进入正题. 什么是listv ...

  7. Is It A Tree?[HDU1325][PKU1308]

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. 简单说说call 与apply

    Function.call() 将函数作为对象的方法调用,例如:function.call(thisobj,args,........); thisobj  调用function的对象.在函数主体中, ...

  9. java.lang.NoSuchMethodError:

    Servlet.service() for servlet [springMVC] in context with path [/mobile] threw exception [Handler pr ...

  10. linux install matlab2014a

    https://pan.baidu.com/s/1qYJ9tNm#list/path=%2F下载镜像文件 2#开始安装,全程最好断网 sudo mkdir /media/matlab sudo mou ...