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.

思路:DP,第n个house如果paint color i, 那第n-1个house就不能。

注意minval1和minval2是前一次dp的最小值,而不是前一个costs的最小值。

 class Solution {
public:
int minCostII(vector<vector<int>>& costs) {
if (costs.size() == || costs[].size() == ) return ;
int N = costs.size();
int K = costs[].size();
vector<vector<int>> dp(N, vector<int>(K, ));
for (int i = ; i < K; i++) dp[][i] = costs[][i];
for (int i = ; i < N; i++) {
int minval1 = INT_MAX, minval2 = INT_MAX;
int minidx1 = , minidx2 = ;
for (int j = ; j < K; j++) {
if (minval1 > dp[i-][j]) {
minval1 = dp[i-][j];
minidx1 = j;
}
}
for (int j = ; j < K; j++) {
if (minval2 > dp[i-][j] && j != minidx1) {
minval2 = dp[i-][j];
minidx2 = j;
}
}
for (int j = ; j < K; j++) {
if (minidx1 == j) dp[i][j] = costs[i][j] + minval2;
else dp[i][j] = costs[i][j] + minval1;
}
}
int minval = INT_MAX;
for (int i = ; i<K; i++) {
minval = min(minval, dp[N-][i]);
}
return minval;
}
};

LC 265. Paint House II的更多相关文章

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

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

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

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

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

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

  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. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

随机推荐

  1. lua table vs closure

    最近在重构自己写的框架中的定时器模块,需要把回调函数保存起来,大概如下: function timer_mgr:save_timer( this,callback ) return { this = ...

  2. 多进程编程——理论讲解与 multiprocessing 模块

    多进程编程 import os pid = os .fork() 功能 :创建新的进程 参数: 无 返回值 :失败返回一个负数 成功:在原有进程中返回新的进程的PID号 在新进程中返回为0* 子进程会 ...

  3. HTML5常用的语义化标签

    快速查询 article | aside | nav | section | header | footer 架构预览 nav 定义导航链接的部分 在页脚显示一个站点的导航链接,如首页.服务信息页面. ...

  4. 7. Function Decorators and Closures

    A decorator is a callable that takes another function as argument (the decorated function). The deco ...

  5. MyBatis-10-多对一处理

    10.多对一处理 多对一: 多个学生,对应一个老师 对于学生这边而言,关联...多个学生,关联一个老师[多对一] 对于老师而言,集合,一个老师又很多学生[一对多] SQL: CREATE TABLE ...

  6. CentOS 6 多实例 编译安装mariadb-5.5.59

    系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的源码包 http://mariadb.org/ mariadb- ...

  7. python改成了python3的版本,那么这时候yum就出问题了

    既然把默认python改成了python3的版本,那么这时候yum就出问题了,因为yum貌似不支持python3,开发了这个命令的老哥也不打算继续写支持python3的版本了,所以,如果和python ...

  8. SpringBoot项目中使用Bootstrap 的CSS、JS资源

    首先 需要在 application.properties 文件中添加这句 spring.mvc.static-path-pattern=/** 不然是使用不了的 还有一种办法就是 使用bootstr ...

  9. 11 git第二部分(未完成)

    https://www.cnblogs.com/shangchunhong/p/9444335.html

  10. [Algorithm] Find The Vowels

    // --- Directions // Write a function that returns the number of vowels // used in a string. Vowels ...