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/

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,451評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,940評論 0 23
  • There are a row of n houses, each house can be painted wi...
    Jeanz閱讀 452評論 0 0
  • There are a row of n houses, each house can be painted wi...
    Jeanz閱讀 259評論 0 0
  • 二丫是個殘疾人。被志愿者們發現的時候,她的雙腿因為肌肉萎縮癥影響,幾乎佝僂成一個語文的“八”字。雙手也因為疾...
    野草凝香閱讀 3,749評論 2 7