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的空間,可以優化空間。
【參考答案】
相關文章
- LeetCode-Paint House IILeetCodeAI
- 【題解】Solution Set - 「藍」題板刷
- Lintcode539 Move Zeroes solution 題解
- Lintcode22 Flatten List solution 題解
- Lintcode387 The Smallest Difference solution 題解
- Android Paint 使用詳解AndroidAI
- Android Paint 之 PathEffect 詳解AndroidAI
- House Of Force
- 【題解】Solution Set - NOIP2024模擬賽4
- 【題解】Solution Set - NOIP2024模擬賽2
- Uber面試題 | 房屋竊賊 House Robber II面試題
- 洛谷 P8192 [USACO22FEB] Paint by Rectangles P 題解AI
- 【題解】Solution Set - NOIP2024集訓Day56 雜湊雜題
- 【題解】Solution Set - NOIP2024集訓Day9 樹上問題
- 【題解】Solution Set - NOIP2024集訓Day57 字串字串
- 【題解】Solution Set - NOIP2024集訓Day26 dp
- LeetCode 動態規劃 House Robber 習題LeetCode動態規劃
- 【題解】Solution Set - NOIP2024集訓Day52 圖論雜題2圖論
- 【題解】Solution Set - NOIP2024集訓Day55 圖論雜題3圖論
- Canvas&Paint 知識梳理(5) Paint#setShaderCanvasAI
- It is the Auction House in Lost Ark
- 【題解】Solution Set - NOIP2024集訓Day71 貪心
- 動態規劃專題之----198. House Robber動態規劃
- 動態規劃專題之----213. House Robber II動態規劃
- Canvas&Paint 知識梳理(4) 影象合成 Paint#setXfermodeCanvasAI
- 【題解】Solution Set - NOIP2024集訓Day44-45 圖論圖論
- 【題解】Solution Set - NOIP2024集訓Day2 線段樹
- 【題解】Solution Set - NOIP2024集訓Day14 CDQ分治
- The House of Mind (FASTBIN METHOD) PRIMEAST
- 【題解】Solution Set - NOIP2024集訓Day7 李超線段樹
- Canvas&Paint 知識梳理(3) 顏色合成 Paint#setColorFilterCanvasAIFilter
- [CareerCup] 9.7 Paint Fill 填充AI
- D. Learning to PaintAI
- LeetCode-House RobberLeetCode
- Nanami and the House Protecting ProblemNaN
- ch09_house_rental_system
- Lintcode208 Assignment Operator Overloading (C++ Only) solution 題解C++
- 【題解】Solution Set - NOIP2024集訓Day20 DP常⻅模型1「序列」模型