Paint House

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

  1. class Solution {
  2. public:
  3. int minCost(vector<vector<int>>& costs) {
  4. if (costs.empty()) return ;
  5. for (int i = ; i < costs.size(); ++i) {
  6. for (int j = ; j < ; ++j) {
  7. costs[i][j] += min(costs[i-][(j+)%], costs[i-][(j+)%]);
  8. }
  9. }
  10. return min(costs.back()[], min(costs.back()[], costs.back()[]));
  11. }
  12. };

Paint House II

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.

Follow up:
Could you solve it in O(nk) runtime?

  1. class Solution {
  2. public:
  3. int minCostII(vector<vector<int>>& costs) {
  4. if (costs.empty() || costs[].empty()) return ;
  5. int n = costs.size(), k = costs[].size();
  6. vector<int> min1(k), min2(k);
  7. for (int i = ; i < costs.size(); ++i) {
  8. min1[] = INT_MAX;
  9. for (int j = ; j < k; ++j) {
  10. min1[j] = min(min1[j-], costs[i-][j-]);
  11. }
  12. min2[k-] = INT_MAX;
  13. for (int j = k - ; j >= ; --j) {
  14. min2[j] = min(min2[j+], costs[i-][j+]);
  15. }
  16. for (int j = ; j < k; ++j) {
  17. costs[i][j] += min(min1[j], min2[j]);
  18. }
  19. }
  20. int res = INT_MAX;
  21. for (auto c : costs.back()) {
  22. res = min(res, c);
  23. }
  24. return res;
  25. }
  26. };

快速找到数组中去掉某个元素的最小值方法:定义两个数组,min1[i]与min2[i]分别记录从左向右到第i位与从右向左到第i位的区间最小值,那么去掉第i位的最小值就是min(min1[i], min2[i])。

[LeetCode] Paint House I & II的更多相关文章

  1. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  4. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  5. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

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

  7. LeetCode Paint House II

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

  8. [LeetCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  9. [LeetCode] Paint House 粉刷房子

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

随机推荐

  1. ViewPage+frament不预载入下一个Frament数据解决的方法

    在做一个ViewPage+Frament 滑动数效果,当滑动到每一页时载入哪一页的数据,可是ViewPage会预载入下一也数据.这个问题之前做项目是一直未解决,今天找到一个方法一下子就解决的这个问题, ...

  2. JavaWeb开发之网站实现文件上传功能

       转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6073505.html  一个功能完善的JavaWeb应用,必不可少的一个功能就是文件的上传.无论是用户的 ...

  3. 用Redis的zset防御Session Flood

    zset (Sorted Set)是set的升级版本, 在set的基础上增加了一个顺序(或者权重)值属性, 属性在添加修改元素时候可以指定. 每次指定后zset会自动重新按新的值调整顺序. 可以理解为 ...

  4. 小米路由Mini刷Breed, 潘多拉和LEDE

    1. 下载breed,地址 http://breed.hackpascal.net/ 2. 下载小米Mini的开发板rom, 地址 http://www1.miwifi.com/miwifi_down ...

  5. HighStock如何动态添加数据组

    var charts = null;        $(function () {            var seriesIds = [830];            // Create the ...

  6. 解决ubuntu13.04 有线网络 时常掉线的问题

    不少朋友在升级或新装ubuntu13.04时遇到有线老掉线的问题:连上不到半分钟又掉了,把网线重新拔插一下又可以接着又掉..基本不能正常使用或工作,很恼人的问题. 网上这方面的资料很少现在我把解决方法 ...

  7. Ubuntu 10.04里安装强大抓图工具Shutter

    Shutter 原名叫GScrot,是一款Linux下很强大的抓图工具,支持截取全屏幕,窗口,自选区域以及网页的图像,并且截取后的图像还可以简单编辑,Shutter还支持插件功能,矩齿,边缘化,碎纸, ...

  8. perf之record

    如果CPU的使用率突然暴涨,如何迅速定位是哪个进程.哪段代码引起的呢?我们需要一个profiling工具,对CPU上执行的代码进行采样.统计,告诉我们CPU到底在忙些什么. perf 就是这样的工具. ...

  9. iOS刻度尺换算之1mm等于多少像素理解

    刚好看到一个刻度尺文章,实现手机屏幕上画刻度尺. 然后就有一个疑问:这个现实中的1mm(1毫米)长度与手机像素之间的换算比怎么来的呢? 看了下demo代码,发现这样写的: CGFloat sc_w = ...

  10. Xcode使用小技巧-filter查找功能和查看最近修改的文件

    今天偶然发现了关于Xcode的一个小技巧: 1.查看最近修改的文件 2.使用filter查找制定文件 没错,就是下面这个东西,很容易忽略的一个小工具,在Xcode左下角位置. 通过这个,我们能够在整个 ...