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. cmake 中的 compile_commands.json 文件

    cmake 是支持多种编译方式的工具,产生多种编译工具可以使用的编译文件,例如常用的gdb. 但是对于clang 编译工具,还需要一个compile_commands.json 这个文件是由cmake ...

  2. 系统学习python第六天学习笔记

    1.补充 1.列表方法补充 reverse,反转. v1 = [1,2,3111,32,13] print(v1) v1.reverse() print(v1) sort,排序 v1 = [11,22 ...

  3. Python笔记_第四篇_高阶编程_反射_(getattr,setattr,deattr,hasattr)

    1. 元数据和反射概念: 有关程序及其类型的数据成为元数据(metadata),他保存在程序的程序集中. 反射这个词儿听起来比较陌生.程序在运行时,可以查看其它程序集或其本身的元数据.一个运行的程序查 ...

  4. Linux用户权限常见命令

    01. 用户 和 权限 的基本概念 1.1 基本概念 用户 是 Linux 系统工作中重要的一环,用户管理包括 用户 与 组 管理 在 Linux 系统中,不论是由本机或是远程登录系统,每个系统都必须 ...

  5. Python3中bytes和HexStr之间的转换

    1 Python3中bytes和HexStr之间的转换 ByteToHex的转换 def ByteToHex( bins ): """ Convert a byte st ...

  6. subprocess.Popen stdout重定向内容实时获取

    python 打开一个新进程执行系统命令, test 执行完才能获取返回, test1 实时获取返回结果 import subprocess def test(cmd): p = subprocess ...

  7. git 提交部分修改的文件,以及如何撤回已经add的文件

    命令 1.git status //查看修改文件状态 ,可以看到哪些add了哪些没add 注意:如果此时出现了有些文件不想添加到暂存区却添加进去了,需要撤回 git reset HEAD 全部撤销gi ...

  8. aiohttp web服务端(server)样例 (非client)

    python版本 python3.6 (其他版本需要小改,版本>python3.4) 参考网址:https://www.cnblogs.com/ameile/p/5589808.html  as ...

  9. 工作小结:Base64注意事项、标签for属性

    Base64 场景1:后台保存的客户填写备注信息,前台无法正常展示 原因:无法正常展示的备注信息为客户直接从黑屏复制过来的信息,信息中包含有不可见的控制字符,回传至前台的json数据,浏览器无法正常解 ...

  10. 获取deeplearning电子书

    deeplearning 电子书 http://www.deeplearningbook.org/ 获取文件名,有顺序 curl http://www.deeplearningbook.org/ | ...