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. day056-58 django多表增加和查询基于对象和基于双下划线的多表查询聚合 分组查询 自定义标签过滤器 外部调用django环境 事务和锁

    一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...

  2. shell(3)拼写检查与词典操作

    1:Linux下,在/usr/share/dict下包含了词典文件,检查一个单词是否在词典里: #!/bin/bash #文件名:checkout.sh #检查给定的单词是否为词典中的单词 word= ...

  3. java 各种循环遍历

    遍历方式选择: 实现了 RandomAccess 接口的 list,优先选择普通 for 循环 ,其次 foreach: 未实现 RandomAccess 接口的 list, 优先选择 iterato ...

  4. 半分钟学会使用markdown基本语法

    想看文字版的看这个吧https://www.jianshu.com/p/191d1e21f7ed

  5. 【浅色】最强Win7 x64评测

    [浅色]最强Win7 x64评测 [浅色]最强Win7 x86 & x64 | WINOS https://www.winos.me/archives/789.htmlESD671MB,安装后 ...

  6. mysql识别中文

    在配置的INI中加上这些 [mysql]default-character-set=utf8no-auto-rehash# Remove the next comment character if y ...

  7. 前端-JavaScript1-3——JavaScript之字面量

    字面量?????? 字面量:英语叫做literals,有些书上叫做直接量.看见什么,它就是什么. 我们先来学习数字的字面量,和字符串的字面量.剩余的字面量类型,我们日后遇见再介绍. 3.1 数字的字面 ...

  8. jquery-2.0.3 源码分析 整体架构

    关键 var jQuery = function( selector, context ) { return new jQuery.fn.init(); } jQuery.fn = jQuery.pr ...

  9. 廖雪峰Java6 IO编程-3Reader和Writer-1Reader

    1.java.io.Reader和java.io.InputStream的区别 InputStream Reader 字节流,以byte为单位 字符流,以char为单位 读取字节(-1,0-255): ...

  10. Make a plan, and stand for it!

    我发现博主本人善于事前做计划,事后做总结.但是不善于坚持自己的计划.就拿10.1这个假期来讲,放假前看多许多的攻略,计划了很多条的自驾出行的路线,但是好像一条也没坚持,最后选择了一条临时的线路,而且临 ...