Question

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.

Follow up:
Could you solve it in O(nk) runtime?

Solution

这里时间复杂度是O(nk),说明要求我们用O(k)的时间计算每一层的新的cost。

cost'[i] = costs[m][i] + min{cost[0], cost[1], ..., cost[i - 1], cost[i + 1], ..., cost[k - 1]}

原想法是对每一个i重新计算,时间复杂度是O(k2)。包含了大量的重复计算

其实我们只需求出cost[]序列的最小值和第二小的值。Time complexity O(k)

 public class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length < 1) {
return 0;
}
int m = costs.length, k = costs[0].length;
int[] cost = new int[k];
int[] tmp = new int[k];
for (int i = 0; i < k; i++) {
cost[i] = costs[m - 1][i];
}
for (int i = m - 2; i >= 0; i--) {
// calculate most and second minimum number
int[] min = calcMin(cost);
for (int j = 0; j < k; j++) {
// if cost[j] is minimum, then add second minimum with costs[i][j]
if (cost[j] == min[0]) {
cost[j] = min[1] + costs[i][j];
} else {
// if cost[j] is not minimum, then add minimum with costs[i][j]
cost[j] = min[0] + costs[i][j];
}
}
}
if (k < 2) {
return cost[0];
}
int[] result = calcMin(cost);
return result[0];
} private int[] calcMin(int[] nums) {
if (nums == null || nums.length < 2) {
return new int[0];
}
int[] mins = new int[2];
mins[0] = Math.min(nums[0], nums[1]);
mins[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
if (nums[i] < mins[0]) {
mins[1] = mins[0];
mins[0] = nums[i];
} else if (nums[i] < mins[1]) {
mins[1] = nums[i];
}
}
return mins;
}
}

Paint House II 解答的更多相关文章

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

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

  3. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

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

  5. LeetCode Paint House II

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

  6. 265. Paint House II

    题目: There are a row of n houses, each house can be painted with one of the k colors. The cost of pai ...

  7. [LeetCode#265] Paint House II

    Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...

  8. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  9. [Swift]LeetCode265.粉刷房子 II $ 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 ...

随机推荐

  1. socket编程2

    package tcp; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExceptio ...

  2. java foreach记录

    实现原理解释: http://blog.csdn.net/a596620989/article/details/6930479 http://stackoverflow.com/questions/8 ...

  3. Java随机数生产算法

    java提供了Math.random()函数,返回一个double类型的随机数,也有util包里的Random类,可以生成double,int,float,long,bytes等随机数. 但有些业务需 ...

  4. Mac Dock 效果及原理(勾股定理)

    这个是苹果机上的 Dock 效果,Windows 上也有一款专门的模拟软件——RocketDock. 代码如下: <!doctype html> <html> <head ...

  5. freemarker字符串转换成日期和时间

    freemarker字符串转换成日期和时间 1.日期时间转换总结 (1)date用来转换为日期 (2)time用来转换为时间 (3)datetime用来转换为日期和时间 2.展示演示样例 <#- ...

  6. html hack 列表

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![ ...

  7. 关于为什么RAID5往往掉一个盘后第二个盘也立刻挂掉的原因分析

    很多人遇到过服务器RAID5挂掉,往往掉一个盘后,第二个盘也立刻挂掉. 大家都知道RAID5 一次允许一个盘缺失, RAID 5也是以数据的校验位来保证数据的安全,但它不是以单独硬盘来存放数据的校验位 ...

  8. 如何让MyEclispe中英文切换

    我们通过网上的一些汉化办法汉化了我们的MyEclipse,可是我们有时候想切回英文版怎么办? 方法一:我们可以通过修改MyEclipse配置文件的办法来从中文恢复到英文, -Duser.languag ...

  9. uva 846 - Steps

    找出步數與距離的關係即可得解. 0步最多能抵達的距離是0 1步最多能抵達的距離是1(1) 2步最多能抵達的距離是2(1 1) 3步最多能抵達的距離是4(1 2 1) 4步最多能抵達的距離是6(1 2 ...

  10. Centos 6.x 系统下用yum安装Memcache

    我们的第一步就是导入第三方软件仓库RPMForge ,首页进行centos 官网找到RPMForge下载地址 http://wiki.centos.org/AdditionalResources/Re ...