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.

题意:

一排有n套房子,每个房子可以选一种颜色(红色、蓝色或者绿色)来粉刷。由于每个房子涂某种颜色的花费不同,求相邻房子不同色的粉刷最小花费。

Solution1: DP

scan一遍n套房子:

costs[i][0]               =      costs[i][0]                   +             min(costs[i-1][1],  costs[i-1][2])

当下选0号色的花费(和)     =  当下选0号色的花费(单价)    +      前套房子选1号色或者选2号色中较小花费

code

 class Solution {
public int minCost(int[][] costs) {
if(costs == null || costs.length == 0 || costs[0].length < 3) return 0; for(int i = 1; i < costs.length; i++ ){ // 照顾[i-1]所以 i 从 1 开始, 则 i - 1 从 0 开始。若从0开始,i 就只能取到costs.length-1
costs[i][0] += Math.min(costs[i-1][1], costs[i-1][2]);
costs[i][1] += Math.min(costs[i-1][0], costs[i-1][2]);
costs[i][2] += Math.min(costs[i-1][1], costs[i-1][0]);
}
int n = costs.length-1;
return Math.min(Math.min(costs[n][0], costs[n][1]), costs[n][2]);
}
}

[leetcode]256. Paint House粉刷房子(三色可选)的更多相关文章

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

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

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

  4. [LintCode] Paint House 粉刷房子

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

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

  6. [LeetCode] 276. Paint Fence 粉刷篱笆

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

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

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

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

随机推荐

  1. 使用deb 打包开发的postgres extension 另外一种方法

    已经写过一个deb 包打包的方法,我们同时也可以使用dpkg-deb 命令 安装依赖工具包 推荐安装全点的 sudo apt-get install build-essential autoconf ...

  2. WinFormEx

    项目地址 :  https://github.com/kelin-xycs/WinFormEx WinFormEx 一个 用 C# 写的 WinForm 扩展库 , 用于改善 WinForm 的 界面 ...

  3. 2.3 Visio画虚线后插入word或PPT变为实线

    选中实线后,左键选择->格式->线条->粗细->自定义->设置为0pt

  4. python学习路程1

    常用的转义字符还有: \n 表示换行 \t 表示一个制表符 \\ 表示 \ 字符本身 任务 请将下面两行内容用Python的字符串表示并打印出来: Python was started in 1989 ...

  5. mysql插中文出现错误 "incorrect string value:\x.....

    mysql字符集的问题: mysql的表格整理应该改为gbk_chinese_ci,每个需要汉字的字段属性的整理也改为gbk_chinese_ci,

  6. buffers和cached的区别

    原文:https://www.cnblogs.com/kevingrace/p/5991604.html buffers和cached解释 ============================== ...

  7. 关于CoreData的用法

    有些同事觉得CoreData是一个看不懂,理解不清的神秘东东,其实ios的本地数据储存是一个sqlite数据库,一个简易的数据库,而这个CoreData是否支持所有储存的数据呢,显然不是的,站在我的角 ...

  8. day34进程相关

    进程1 什么是进程    进程指的是一个正在进行/运行的程序,进程是用来描述程序执行过程的虚拟概念    进程vs程序    程序:一堆代码    进程:程序的执行的过程    进程的概念起源于操作系 ...

  9. 2.pandas数据清洗

    pandas是用于数据清洗的库,安装配置pandas需要配置许多依赖的库,而且安装十分麻烦. 解决方法:可以用Anaconda为开发环境,Anaconda内置了许多有关数据清洗和算法的库. 1.安装p ...

  10. 6. 添加messager.alert()确定按钮的回调函数,即点完确定按钮后触发的事件

    添加messager.alert()确定按钮的回调函数,即点完确定按钮后触发的事件: $.messager.alert('提示信息', "请联系管理员处理!", 'info', f ...