[抄题]:

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.

Example:

Input: [[1,5,3],[2,9,4]]
Output: 5
Explanation: Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5;
  Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

k个颜色就不知道怎么办了:还是试啊 再套一层循环 一个个加

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

三重循环, s 和 j相等的时候就continue掉

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. i j是主变量,所以cost[i][j]都得用, dp[i][j]数组在不变的情况下就是它自己本身
dp[i][j] = Math.min(dp[i][j], dp[i - 1][s] + costs[i][j]);

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

所以cost[i][j]都得用, dp[i][j]数组在不变的情况下就是它自己本身

[复杂度]:Time complexity: O(n*k*k) Space complexity: O(n*k)

[算法思想:迭代/递归/分治/贪心]:

贪心

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public int minCostII(int[][] costs) {
//cc
if (costs == null || costs.length == 0) return 0; //ini: dp[][], dp[0][k]
int n = costs.length, k = costs[0].length;
int[][] dp = new int[n][k];
for (int j = 0; j < k; j++) {
dp[0][j] = costs[0][j];
} //for loop: continue;
for (int i = 1; i < n; i++) {
for (int j = 0; j < k; j++) {
dp[i][j] = Integer.MAX_VALUE;
for (int s = 0; s < k; s++) {
if (s == j) continue;
dp[i][j] = Math.min(dp[i][j], dp[i - 1][s] + costs[i][j]);
}
}
} //return: compare each costs[i][k]
int res = Integer.MAX_VALUE;
for (int j = 0; j < k; j++) {
res = Math.min(res, dp[n - 1][j]);
} return res;
}
}

265. Paint House II 房子涂色K种选择的版本的更多相关文章

  1. 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:二叉树下的不能相邻,求能 ...

  2. [leetcode]265. Paint House II粉刷房子(K色可选)

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  3. [LeetCode] 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 paintin ...

  4. [leetcode]256. Paint House粉刷房子(三色可选)

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  5. 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 ...

  6. [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 ...

  7. LC 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 paintin ...

  8. [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 ...

  9. 【BZOJ-1260】涂色paint 区间DP

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1147  Solved: 698[Submit][Sta ...

随机推荐

  1. 爸妈才是最好的避孕药--------"北大状元拉黑父母事件的一些感想"

    今天看了这么一篇文章,地址:  http://mini.eastday.com/mobile/180131180318786.html <北大状元拉黑父母6年:你敢恨爸妈,可你敢原谅他们吗?&g ...

  2. 控制led灯并显示自己的数值

    前提是有led.jar包封装好了东西 1.并设置好led灯的模式并打开串口. 2.在布局中创建一个EditText ,Button.并利用-.getText.toString().trim(); 来获 ...

  3. BZOJ3207: 花神的嘲讽计划Ⅰ(hash)

    3207: 花神的嘲讽计划Ⅰ Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3569  Solved: 1258[Submit][Status][Di ...

  4. 用yum安装lamp和lnmp环境

    LAMP: 1.首先安装Apache和mysql: yum install -y httpd mysql mysql-server mysql-devel 2.启动httpd和mysqld: serv ...

  5. Linux下sniffer实现(转)

    转发网址:https://blog.csdn.net/eqiang8271/article/details/8489769 //Example 1. #include <stdio.h> ...

  6. python和C语言互相调用的几种方式

    ? 1 2 3 4 5 6 7 8 9 版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址   http://www.cnblogs.com/Colin-Cai/ ...

  7. mysql having,group by查询去除重复记录

    http://m.jb51.net/article/39302.htm 可以这样去理解group by和聚合函数 http://www.cnblogs.com/wuguanglei/p/4229938 ...

  8. CF 914G Sum the Fibonacci——子集卷积

    题目:http://codeforces.com/contest/914/problem/G 第一个括号可以子集卷积:第三个括号可以用 FWT 异或卷积:这样算出选两个数组成 x 的方案数:三个部分的 ...

  9. 手淘flexible.js框架使用和源代码讲解

    手淘框架是一个用来适配移动端的js框架,下面我们来讲解一下如何使用手淘的这套框架. 其实手淘框架的核心原理就是根据不同的width给网页中html跟节点设置不同的font-size,然后所有的距离大小 ...

  10. C语言 字符串处理函数 转自 http://blog.chinaunix.net/uid-25885064-id-3175049.html

     C字符串处理函数 2012-04-13 18:14:16 分类: C/C++ void *memccpy (void *dest, const void *src, int c, size_t n) ...