文章标题
来源:互联网 发布:情定三生向天和知夏 编辑:程序博客网 时间:2024/05/09 15:45
Description
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 3 cost matrix. For example, costs0 is the cost of painting house 0 with color red; costs1 is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.Note: All costs are positive integers.
Solution
复杂度
时间 O(N) 空间 O(1)
思路
直到房子i,其最小的涂色开销是直到房子i-1的最小涂色开销,加上房子i本身的涂色开销。但是房子i的涂色方式需要根据房子i-1的涂色方式来确定,所以我们对房子i-1要记录涂三种颜色分别不同的开销,这样房子i在涂色的时候,我们就知道三种颜色各自的最小开销是多少了。我们在原数组上修改,可以做到不用空间。
public class Solution { public int minCost(int[][] costs) { if(costs != null && costs.length == 0) return 0; for(int i = 1; i < costs.length; i++){ // 涂第一种颜色的话,上一个房子就不能涂第一种颜色,这样我们要在上一个房子的第二和第三个颜色的最小开销中找最小的那个加上 costs[i][0] = costs[i][0] + Math.min(costs[i - 1][1], costs[i - 1][2]); // 涂第二或者第三种颜色同理 costs[i][1] = costs[i][1] + Math.min(costs[i - 1][0], costs[i - 1][2]); costs[i][2] = costs[i][2] + Math.min(costs[i - 1][0], costs[i - 1][1]); } // 返回涂三种颜色中开销最小的那个 return Math.min(costs[costs.length - 1][0], Math.min(costs[costs.length - 1][1], costs[costs.length - 1][2])); }}
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 吃土豆
- 多媒体(视频播放器)
- hibernate面试题 hibernate的工作流程、hibernate的查询数据方式
- xmlhttp实现文件上传
- 大数据HBase分布式部署之【二】:zookeeper-3.4.9安装配置!
- 文章标题
- 当前使用的功能与其他用户有冲突,目前无法使用--kingdee k3
- 对象的创建和生命期
- Linux下查看内存使用情况方法总结
- 哈希表(Hashtable)
- Bootstrap modal拖动效果
- 大家快来A水题
- http code状态码,错误码
- VBA中msgbox的用法小结