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. 《周易》中的君子形象--http://cul.china.com.cn/guoxue/2018-06/04/content_40369049.htm

    中国文学本质上是一种君子文学,君子是中国文学的创作主体,君子与小人的人格冲突是中国文学矛盾冲突的主要形式.最早的君子是居住于城邦的贵族,而西周以来这一语词的道德化倾向愈来越重,渐渐摆脱了阶级意义而成为 ...

  3. 【PXC】关于限流的参数,状态值说明

    一.什么是流控(FC)?如何工作? 节点接收写集并把它们按照全局顺序组织起来,节点将接收到的未应用和提交的事务保存在接收队列中,当这个接收队列达到一定的大小,将触发限流:此时节点将暂停复制,节点会先处 ...

  4. miniconda安装jupyter

    1.安装jupyter 由于miniconda是anaconda的简化版,只有一个prompt: 安装jupyter,只需要打开prompt的dos窗口,输入命令pip install jupyter ...

  5. Chrome使用频率最高的快捷键

    标签 ctrl+T 打开新标签  ——— ctrl+W 关闭标签 ctrl+shift+T 打开上衣个被关闭的标签 ctrl+tab 标签向右切换 —— ctrl+shift+tab 标签向左切换 c ...

  6. PowerShell创建 Profile

    profile主要用于个性化常用的函数.别名等等.每次加载powershell的时候,都会执行profile中的内容. 查看是否有profile: $profile 如果结果是false说明没有.则创 ...

  7. 上传本地项目到GIT码云

    1.下载GIT 下载地址:https://git-scm.com/downloads 我这里下载的64位 2.安装GIT 双击下载的Git-2.18.0-64-bit.exe文件,选择自己的安装目录, ...

  8. 35)类和结构体类比---this

    那么,为啥  Test a(10)  , Test  b(20)   然后  我a.getI()  取到的是10,而不是20     就能将那个  10  给  a  对象的  m1    是因为有 ...

  9. ruoyi IpUtils

    package com.ruoyi.common.utils; import java.net.InetAddress; import java.net.UnknownHostException; i ...

  10. 吴裕雄--天生自然python TensorFlow图片数据处理:解决TensorFlow2.0 module ‘tensorflow’ has no attribute ‘python_io’

    tf.python_io出错 TensorFlow 2.0 中使用 Python_io 暂时使用如下指令: tf.compat.v1.python_io.TFRecordWriter(filename ...