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.

Example:

Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
  Minimum cost: 2 + 5 + 3 = 10.
class Solution {
public int minCost(int[][] costs) {
if (costs == null || costs.length == 0 || costs[0].length == 0) {
return 0;
}
int curRed = costs[0][0];
int curBlue = costs[0][1];
int curGreen = costs[0][2];
int prevRed = curRed;
int prevBlue = curBlue;
int prevGreen = curGreen;
for (int i = 1; i < costs.length; i++) {
curRed = Math.min(prevBlue, prevGreen) + costs[i][0];
curBlue = Math.min(prevRed, prevGreen) + costs[i][1];
curGreen = Math.min(prevRed, prevBlue) + costs[i][2];
prevRed = curRed;
prevBlue = curBlue;
prevGreen = curGreen;
}
return Math.min(curRed, Math.min(curBlue, curGreen));
}
}

[LC] 256. Paint House的更多相关文章

  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. 256. Paint House房屋染色

    [抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...

  3. 256. Paint House

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

  4. [LeetCode#256] Paint House

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

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

  6. [LeetCode] 256. Paint House_Easy tag: Dynamic Programming

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

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

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

  9. 【LeetCode】256. Paint House 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

随机推荐

  1. I420转RGB

    http://blog.csdn.net/huiguixian/article/details/17288909 public class YuvToRGB { private static int  ...

  2. PAT Basic 1047 编程团体赛(20) [Hash散列]

    题目 编程团体赛的规则为:每个参赛队由若⼲队员组成:所有队员独⽴⽐赛:参赛队的成绩为所有队员的成绩和:成绩最⾼的队获胜.现给定所有队员的⽐赛成绩,请你编写程序找出冠军队. 输⼊格式: 输⼊第⼀⾏给出⼀ ...

  3. 求素数的一个快速算法 Python 快速输出素数算法

    思想 以100以内为例. 生成一个全是True的101大小的数组 2开始,遇到2的倍数(4,6,8,10...)都赋值为False 因为这些数字都有因子 2 3开始,遇到3的倍数(6,9,12...) ...

  4. Python dict 字典 keys和values对换

    原字典: d1 = { 'en':'英语', 'cn':'中文', 'fr':'法语', 'jp':'日语' } 经过相互对换: d1_inverse = {values:keys for keys, ...

  5. gff文件提取cds

    #!/usr/bin/perl use strict; use warnings; ########input######## ];my $cut = &cut($gff);my %cut = ...

  6. React 渲染嵌套对象,内部对象会是undefined

    在编译器中获取数据 发现报错 原因: render()一加载就会渲染,渲染的数据是初始state里的值 ,当setState会再次渲染 解决方法 1.三元运算 判断对象是否存在 2.在初始化对象的时候 ...

  7. 中国文化产业基金越来越多,但IP变现难题为何仍未解决?

    自始至终,中国商界领域的态势就有一个很明显的特征--哪里是风口.哪里是热点,企业就会蜂拥而至并集体掘金.一直到决出胜负,或者把整个风口"做烂"才罢休.很典型的案例就是电商领域已经呈 ...

  8. Linux-使用syslog记录调试信息

    1.有三个函数:openlog.syslog.closelog 2.一般的log信息都在操作系统的/var/log/messages这个文件中存储着,但是ubuntu中是在/var/log/syslo ...

  9. Spring的设计理念和整体架构

    1.Spring的各个子项目 Spring Framework(Core):这是我们熟知的Spring项目的核心.Spring Framework(Core)中包含了一系列Ioc容器的设计,提供了依赖 ...

  10. python图像处理常用方法

    在线标注网站 https://gitlab.com/vgg/via http://www.robots.ox.ac.uk/~vgg/software/via/via.html 数组与图像互转 from ...