LintCode刷题笔记-- PaintHouse 1&2
标签:
动态规划
题目描述:
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 1with color 2, and so on... Find the minimum cost to paint all houses.
Example
Given n = 3, k = 3, costs =[[14,2,11],[11,14,5],[14,3,10]] return 10
house 0 is color 2, house 1 is color 3, house 2 is color 2,2 + 5 + 3 = 10
解题思路:
这两题较比之前的题目来讲要简单很多,子状态非常明显,转移方程非常容易得到
1.对于每一个房子,都k种设计方案。第n间房子的每种颜色的方案依赖于,第n-1间房子的其他颜色的最省钱方案。
2.初始状态,只有一间房子的时候,颜色价格方案是已知的。
参考代码:
public int minCostII(int[][] costs) {
// Write your code here
if(costs.length == 0||costs[0].length == 0) return 0;
int n = costs.length;
int k = costs[0].length;
int[][] dp = new int[n][k];
for(int i = 0; i<k; i++){
dp[0][i] = costs[0][i];
}
for(int i=1; i<n; i++){
for(int j = 0; j<k; j++){
int tmp_min = Integer.MAX_VALUE;
for(int m = 0; m<k; m++){
if(m==j){
continue;
}else{
tmp_min = Math.min(tmp_min, dp[i-1][m]);
}
}
dp[i][j] = tmp_min+costs[i][j];
}
}
int min = Integer.MAX_VALUE;
for(int i=0; i<k; i++){
min = Math.min(min, dp[n-1][i]);
}
return min;
}
LintCode刷题笔记-- PaintHouse 1&2的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
随机推荐
- nodejs vue-cli 微信公众号开发(一) 申请域名搭建服务器
一.搭建本地服务器 1.首先保存本地的80端口被node监听,利用内网穿透工具把80端口映射出去.(ngrok工具可以穿透内网使本地ip作为外网使用) 2.打开https://natapp.cn/tu ...
- MSSQLSERVER跨服务器连接(远程登录)的示例代码
MSSQLSERVER跨服务器链接服务器创建方法如下 复制代码 代码如下: --声明变量 Declare @svrname varchar(255), @dbname varchar(255), @s ...
- F. Cowmpany Cowmpensation dp+拉格朗日插值
题意:一个数,每个节点取值是1-d,父亲比儿子节点值要大,求方案数 题解:\(dp[u][x]=\prod_{v}\sum_{i=1}^xdp[v][i]\),v是u的子节点,先预处理出前3000项, ...
- 杂项-公司:Axway
ylbtech-杂项-公司:Axway Axway 公司是法国Sopra 集团从事应用系统集成(EAI/B2Bi)软件及相关咨询服务业务的全资子公司.Axway公司成立于1980年,总部位于美国凤凰城 ...
- disruptor 高效队列
disruptor 是什么: disruptor 是一个 低延时的 无锁 环形 队列. 相较于 java的 队列 ,他有明显的优点 ,无界,无锁,低延时(解决了为内存共享问题 ) disrupto ...
- MyBatis与JPA的区别
参考博客: https://www.cnblogs.com/llywy/p/10103136.html https://www.jianshu.com/p/32ce87c163d6 MyBatis分为 ...
- 解决pycharm安装python库报错问题
最近在玩微信图灵机器人,不过我安装有一些库,安装报错,上网找了很久,总结有两种方法,记录一下 方法一: 手动安装,直接到官网你需要的python库下载到本地, 放在安装python路径,C:\User ...
- 用Axure RP7创建响应式原型教程
自从几年前响应式技术开始应用时,创建响应式原型就成为了很多人苦恼的事情.响应式设计用一种非常优雅的方式处理为多种设备类型使用HTML和CSS编码的应用,但是提供给UX专业人士的原型工具却没有具备同样品 ...
- WPF界面设计中常用的一些代码片段及属性
一.窗体去掉标题栏.消除掉标题栏后的白边,把窗体置于屏幕中间,窗口大小不能改变. WindowStyle="None" AllowsTransparency="True& ...
- typeof 、Object.prototype.toString和 instanceof
数据类型 js 基本类型包括:Undefined symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...