Paint House II 解答
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 解答的更多相关文章
- [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 ...
- 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:二叉树下的不能相邻,求能 ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [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 ...
- LeetCode Paint House II
原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...
- 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 ...
- [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 ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- [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 ...
随机推荐
- 【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby
要安装ruby,首先要安装rvm,借助rvm安装ruby rvm 的全称是 Ruby Version Manager ,是一款由 Wayne E. Seguin 开发的一款命令行工具.rvm 能够让 ...
- HTML获取用户输入的几种玩法
input标签 input是一个自闭和标签,可以获得用户的输入 form标签 form标签是用来进行表单提交用的,它把用户的输入内容提交到服务器. 一个注册页面的例子 <!DOCTYPE htm ...
- Angular基础教程:表达式日期格式化[转]
本地化日期格式化: ({{ today | date:'medium' }})Nov 24, 2015 2:19:24 PM ({{ today | date:'short' }})11/24/15 ...
- 有利于SEO的DIV+CSS规范小结
一.CSS文件及样式命名 1.CSS文件命名规范 全局样式:global.css:框架布局:layout.css:字体样式:font.css:链接样式:link.css:打印样式:print.css: ...
- jQuery插件开发 格式与解析3之$.extend()用途
前叙:$.extend()——用途:扩展和继承 1.Object extend() 用一个或多个对象扩展另一个对象,并返回已修改的原始对象.这对于简单继承是一个非常有用的实用工具. (1)扩展:(Do ...
- 关于ubuntu16.04给firefox安装flash的补充
这两天把自己的老笔记本安装了ubuntu的16.04版本,关于给firefox安装flash player的方法,网上有很多,但不知道是版本还是其它原因,他们文章都出现目录错误的问题,我个人由于是ub ...
- canvas--画宇宙
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 初学Java ssh之Spring 第四篇
今天我来学习学习Spring中Bean. 在Spring中原来还有<beans.../>标签啊,它相当于<bean.../>标签的老爸,老爸可以有很多个儿子,但是老爸只有一个哦 ...
- Cookie[1]
1.什么是Cookie Cookie是一小段文本类型的数据,由服务器发送,并保留在客户端的计算机上. 2.Cookie的作用 服务器可以利用Cookie包含的信息来筛选并经常维护这些信息,以判断在Ht ...
- Google map实现类似Google earth的图标展开功能
前言 在Google map的开发时,如果有多个图标重叠在一起,这时下面的图标就点击不到.而在Google Earth中,鼠标一移到上面就会自动弹开,这对于用户而言是十分人性化的一个功能.如下是在Go ...