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 ...
随机推荐
- Vim ---- 默认打开行号
Vim有非常迅速跳转到某一行行首的方法,例如 :n 或者 nG,n 表示到第 n 行. 但是Vim的显示行号功能默认是关闭的. 可用一下方法使Vim默认显示行号: 在配置文件 .vimrc 中,输入 ...
- ThreadLocal简析
简介 ThreadLocal在Java多线程开发中常见的一个类,在面试中也经见的问题,比如ThreadLocal的作用是什么,ThreadLocal的实现原理是什么等等.ThreadLocal是jav ...
- natapp出现Invalid Host header
前端是vue 3.x 项目,需要更改vue的配置文件 vue.config.js,在module.exports中添加devServer:{disableHostCheck:true}
- arguments的介绍(一)
arguments 是一个类数组对象.代表传给一个function的参数列表. 1.1 arguments length arguments 是个类数组对象,其包含一个 length 属性,可以用 a ...
- 反编译之jd-gui的安装
1.下载JD-GUI http://jd.benow.ca/ 2.下载的dmg安装一直失败 通过brew(https://brew.sh/index_zh-cn.html)命令安装 brew cas ...
- hadoop2.x 完全分布式详细集群搭建(图文:4台机器)
在准备之前说一下本次搭建的各节点角色,进程. nameNode 进程:NameNode dataNode 进程:DataNode resourceManager :ResourceManager n ...
- 2019-3-1-VisualStudio-扩展开发-获得输出窗口内容
title author date CreateTime categories VisualStudio 扩展开发 获得输出窗口内容 lindexi 2019-03-01 09:21:41 +0800 ...
- Chsh- Linux必学的60个命令
1.作用 chsh命令的作用是更改使用者shell设定,它的使用权限是所有使用者. 2.格式 chsh [ -s ] [ -list] [ --help ] [ -v ] [ username ] 3 ...
- SPOJ - UOFTCG 树的最小路径覆盖
//SPOJ - UOFTCG 树的最小路径覆盖 #include <bits/stdc++.h> #include <vector> using namespace std; ...
- Linux下使用SSH命令行传输文件到远程服务器
目标:CentOS 7 调整 home分区 扩大 root分区 总体过程: 把/home内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/root文件系统,新建/home ,恢复/home内容 ...