原题链接在这里: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. iOS开发的小技巧

    转自简书:http://www.jianshu.com/p/50b63a221f09  http://www.jianshu.com/p/08f194e9904c 原作者:叶孤城___ self.ta ...

  2. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

  3. kNN算法python实现和简单数字识别

    kNN算法 算法优缺点: 优点:精度高.对异常值不敏感.无输入数据假定 缺点:时间复杂度和空间复杂度都很高 适用数据范围:数值型和标称型 算法的思路: KNN算法(全称K最近邻算法),算法的思想很简单 ...

  4. [IDEA] 快捷键学习

    IntelliJ Idea 常用快捷键列表   Alt+回车 导入包,自动修正Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码 Ctrl+Alt+O 优化导 ...

  5. Struts2 文件上传和文件下载

    一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...

  6. linux系统meminfo详解(待补充)

    ========================================================================================== MemTotal: ...

  7. 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest

    A. Altitude 从小到大加入每个数,用set查找前驱和后继即可. 时间复杂度$O(n\log n)$. #include <bits/stdc++.h> using namespa ...

  8. 2014亚马逊在线笔试题目及解决方案(MMChess问题)

    整体思路:关键是需要知道当前Steps数组中的全排列即可,而且需要不重复的全排列.即关键在于非递归的全排列实现即可~ 其实直接利用STL中的next_permutation算法的,这里我又自己实现了一 ...

  9. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

  10. 在docker 中配置hadoop1.2.1 cluser

    最近一直在找工作,比较空闲,就没事研究一下hadoop,网上的视频及书,讲的差不多都是1.2.1这个版本,然后就试着在docker中搭建了一个hadoop集群, 项目已经放到了github上面了,供新 ...