LeetCode 265. Paint House II(房子涂色)
2016-04-10 16:22
591 查看
原题网址: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
matrix. For example,
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.
Follow up:
Could you solve it in O(nk) runtime?
方法:动态规划,求出最低成本和次低成本,如果房子的颜色和上一个房子最低成本的颜色相同,则涂上次低成本的颜色,否则涂最低成本的颜色。
这道题不需要我们分别求出与每一个颜色不同的最低成本值,只需要求出最低和次低,就可以有效避免颜色间隔的问题,这是关键。
可以进一步优化得更简洁:
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 kcost
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.
Follow up:
Could you solve it in O(nk) runtime?
方法:动态规划,求出最低成本和次低成本,如果房子的颜色和上一个房子最低成本的颜色相同,则涂上次低成本的颜色,否则涂最低成本的颜色。
这道题不需要我们分别求出与每一个颜色不同的最低成本值,只需要求出最低和次低,就可以有效避免颜色间隔的问题,这是关键。
public class Solution { private void find(int[] cost, int[] min, int[] color) { color[0] = -1; color[1] = -1; for(int i=0; i<cost.length; i++) { if (color[0] == -1 || cost[i] < min[0]) { min[0] = cost[i]; color[0] = i; } } for(int i=0; i<cost.length; i++) { if (i != color[0] && (color[1] == -1 || cost[i] < min[1])) { min[1] = cost[i]; color[1] = i; } } } public int minCostII(int[][] costs) { if (costs == null || costs.length == 0) return 0; int[] minCost = new int[2]; int[] color = new int[2]; int[] nextMinCost = new int[2]; int[] nextColor = new int[2]; find(costs[0], minCost, color); for(int i=1; i<costs.length; i++) { for(int j=0; j<costs[i].length; j++) { costs[i][j] += j!=color[0] ? minCost[0] : minCost[1]; } find(costs[i], nextMinCost, nextColor); int[] t1 = minCost; minCost = nextMinCost; nextMinCost = t1; int[] t2 = color; color = nextColor; nextColor = t2; } return minCost[0]; } }
可以进一步优化得更简洁:
public class Solution { // 求出最低成本和次低成本 private void mins(int[] cost, int[] min) { for(int i=0; i<cost.length; i++) { if (i==0 || cost[i] < cost[min[0]]) min[0] = i; } if (min[0] == 0) min[1] = 1; else min[1] = 0; for(int i=0; i<cost.length; i++) { if (i != min[0] && cost[i] < cost[min[1]]) min[1] = i; } } public int minCostII(int[][] costs) { if (costs == null || costs.length == 0) return 0; int[] minCost = new int[costs[0].length]; int[] min = new int[2]; for(int c=0; c<costs[0].length; c++) { minCost[c] = costs[0][c]; } mins(minCost, min); for(int h=1; h<costs.length; h++) { int[] prevCost = minCost; minCost = new int[costs[0].length]; for(int c=0; c<costs[h].length; c++) { if (c != min[0]) minCost[c] = prevCost[min[0]] + costs[h][c]; else minCost[c] = prevCost[min[1]] + costs[h][c]; } mins(minCost, min); } return minCost[min[0]]; } }
相关文章推荐
- Google用不了解决方法(添加gmail用不了的方法)
- mybaits注解
- 非常阔气的Raid磁盘阵列
- email正则表达式 验证邮箱格式
- [人工智能]IBM Watson人工智能API|一步步创建智能微信翻译官|第一章
- 2014年山东省第五届ACM--angry_birds_again_and_again
- mybaits 框架运用
- Mybaits学习过程中出现的错误
- 题目1436:Repair the Wall
- STL学习笔记— —无序容器(Unordered Container)
- Pass-Muraille
- Deeplearning ,AI,Machine Learning 的关系
- [www.51kuaisou.com]最全的maven中央仓库
- 如何逃过taint droid的跟踪
- 一张图系列——从CreateProcess到main函数的过程
- POJ 3421 X-factor Chains
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案
- Rails的一些設定、Bundler,以及Command Line指令的用法
- CF 346 B vector<pair> s[100]
- POJ2585 Window Pains