Lintcode515 Paint House solution 题解

【题目描述】

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.

 Notice

All costs are positive integers.

这里有n个房子在一列直线上,现在我们需要给房屋染色,分别有红色蓝色和绿色。每个房屋染不同的颜色费用也不同,你需要设计一种染色方案使得相邻的房屋颜色不同,并且费用最小。

费用通过一个nx3 的矩阵给出,比如cost[0][0]表示房屋0染红色的费用,cost[1][2]表示房屋1染绿色的费用。

 注意事项

所有费用都是正整数

【题目链接】

www.lintcode.com/en/problem/paint-house/

【题目解析】

这道题只有3种颜色,所以很简单。dp[i][j]表示第i幢房子涂j的颜色最小的总和,即从前一幢房子的状态dp[i-1][] (k != j)中选一个最小的再加上给第i幢房子涂j颜色的cost。如果直接在costs上修改,则不用单独开dp的空间,可以优化空间。

【参考答案】

www.jiuzhang.com/solutions/paint-house/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,169评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,494评论 0 23
  • There are a row of n houses, each house can be painted wi...
    Jeanz阅读 3,229评论 0 0
  • There are a row of n houses, each house can be painted wi...
    Jeanz阅读 1,774评论 0 0
  • 二丫是个残疾人。被志愿者们发现的时候,她的双腿因为肌肉萎缩症影响,几乎佝偻成一个语文的“八”字。双手也因为疾...
    野草凝香阅读 9,272评论 2 7

友情链接更多精彩内容