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 1
with 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 ...
随机推荐
- 16进制与utf-8
很多人将数据的存储.传输方式和展现形式混为一谈. 类似的16进制 2进制是讲内容在电脑里面的存储或者传输的一种格式, 而utf-8 gb2312 等是输出的展现的一种格式 不是一回事,另外 gbk包含 ...
- linux centos 恢复 还原 备份 Snapper 快照说明
为什么要使用Snapper快照? 我们可以想像以下场景: 1. 场景一:系统发生意外宕机,工程师无法快速定位问题,业务受到中断,客户十分不满意. 2. 场景二:项目会议上,就是否升级某软件到最新版本, ...
- 图论最短路径算法——Dijkstra
说实在的,这算法很简单,很简单,很简单--因为它是贪心的,而且码量也小,常数比起SPFA也小. 主要思想 先初始化,dis[起点]=0,其它皆为无限大. 还要有一个bz数组,bz[i]表示i是否确定为 ...
- sql.xml where ids in的写法
<if test="iSurfaceTypeArray != null"> AND b.i_SurfaceType in <!-- 根据外观检查查询 --> ...
- HTML 颜色表示
三种表示方法 1 颜色单词 : blue green red pink 2 10进制表示: RGB(255, 10, 0) 3 16进制表示: #FF0000(红) #00FF00(绿)
- <爬虫>用正则爬取B站首页图片
import re import requests headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Apple ...
- 史上最贵域名诞生!360斥资1700万美元买360.com
昨日,360公司官方人士向腾讯科技确认,公司已斥巨资收购国际顶级域名360.com.传闻这一收购价格为1700万美元,约合人民币1.1亿元. 史上最贵域名诞生!360斥资1700万美元买360.com ...
- 将ERF格式转换成PCAP格式
在研究网络流量分析的时候,wireshark默认采用pcap格式.对于用Endace DAG捕捉卡捕获的数据包,一般来说,都是erf格式的.一般来说,此种格式包含了更多了链路层信息.而我们采用wire ...
- PKU OJ A Bug's life
http://bailian.openjudge.cn/tm2018/G/ #include <iostream> #include <vector> #include < ...
- Leetcode 242.有效的字母异位词(Python3)
题目: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram& ...