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 ...
随机推荐
- 基于 canvas 将图片转化成字符画
字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢? HTML5 让这个可能变成了现实,通过 canvas,可以很轻松实现这个功能. 其实原理很简单:扫描图片相应位置的像素点,再计算出其灰 ...
- React问答小demo
在学习react初期,看了一些视频和资料,react基础知识差不多学完,跟着网上的一个教程,做了一个小型的问答demo. 需求看图说: 1.点击"添加"按钮,显示问题输入表单,再次 ...
- Oracle_Q&A_04
2014-12-19作业 [JSU]LJDragon's Oracle course tasks In the first semester, junior year --1.在管理员权限下创建一个新 ...
- Wamp集成环境安装
一.Wamp下载 点我下载WampServer2.1a-x32 二.Wamp安装步骤 三.修改语言为汉语 四.查看测试页面
- [原创作品] 对获取多层json值的封装
今天篇头不废话了,交流加群:164858883 在我们接收后端返回的json数据的时候,在数据缺失的时候,如果直接接收会导致致命错误的发生.可能有些同学会说通常都会有,不用判断直接获取也行.之前我也是 ...
- MySQL添加外键的方法
为book表添加外键: <1>明确指定外键的名称: 语法:alter table 表名 add constraint 外键的名称 foreign key(你的外键字段名) REFERENC ...
- mybatis简单应用(基于配置文件)
本文主要介绍了如何使用mybatis进行简单的数据库操作.本人使用的是mybatis3.05. 1.创建数据库表(User表) CREATETABLE `NewTable` (`userId` big ...
- HTML5硕士学习笔记
如今,该集团经过培训的同事给大家HTML5,他出席了两个5训练日,大概过一次给我们,在一个很形象.同事们更感兴趣的是. 课后共享所有的课件.在热情的新技术,我想工作有一个良好的早晨,我决定重新学习课件 ...
- uploadify3.1 参数 中文详解
langFile: 'http://www.static-xxx.nu/uploader/uploadifyLang_en.js',//语言包的路径,能设置所有的提示文字 swf: 'http://w ...
- Linux命令之必杀绝技Vi文本编辑的使用
vi 文本编辑器 语法:vi [参数] 文件 进入vi全屏幕编辑画面 按字母i进入[输入模式],按[ESC]转到命令行, 输入 :q可不保存退出vi :wq存盘退出vi :q!不存盘强制退出 :w ...