[LinkedIn]Min cost of paint house with color
2015-04-05 09:22
302 查看
见过在onsite的居多
Given a list of houses and the cost of painting each house, the houses can be painted in three colors RED, GREEN and BLUE, two neighboring houses can’t be painted in the same color, calculate the total minimum cost for painting all houses.
解法:
1) Dynamic programming problem.
2)Maintain a array of min cost called min_cost, each column represent a color and each row represent a houses.
3) min_cost[i][0] represents that, when the ith house is painted Red, the min cost of 0 to ith houses. Thus we have the following:
min_cost{i} = min(
//since the current color cannot be the same as the previous one
cost[i][R] + min(cost{i-1, B}, cost{i-1, G}),
cost[i][B] + min(cost{i-1, R}, cost{i-1, G}),
cost[i][G] + min(cost{i-1, R}, cost{i-1, B})
)
Given a list of houses and the cost of painting each house, the houses can be painted in three colors RED, GREEN and BLUE, two neighboring houses can’t be painted in the same color, calculate the total minimum cost for painting all houses.
解法:
1) Dynamic programming problem.
2)Maintain a array of min cost called min_cost, each column represent a color and each row represent a houses.
3) min_cost[i][0] represents that, when the ith house is painted Red, the min cost of 0 to ith houses. Thus we have the following:
min_cost{i} = min(
//since the current color cannot be the same as the previous one
cost[i][R] + min(cost{i-1, B}, cost{i-1, G}),
cost[i][B] + min(cost{i-1, R}, cost{i-1, G}),
cost[i][G] + min(cost{i-1, R}, cost{i-1, B})
)
/* * Code: */ //cost is a #_of_house x 3 array public int minPaintCost(int[][] cost) { if (cost == null || cost.length == 0) return 0; int[][] dp = new int[cost.length][3]; dp[0][0] = cost[0][0], dp[0][1] = cost[0][1], dp[0][2] = cost[0][2]; for (int i = 1; i < cost.length; ++i) { dp[i][0] = cost[i][0] + Math.min(dp[i-1][1], dp[i-1][2]); dp[i][1] = cost[i][1] + Math.min(dp[i-1][0], dp[i-1][2]); dp[i][2] = cost[i][2] + Math.min(dp[i-1][0], dp[i-1][1]); } return Math.min(dp[dp.length-1][0], Math.min(dp[dp.length-1][1],[dp.length-1][2])); }
相关文章推荐
- [Graphics] UIColor created with component values far outside the expected range, Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
- Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP
- ALL Code of color snesor with arduino
- FollowMe: Efficient Online Min-Cost Flow Tracking with Bounded Memory and Computation
- HousePrice of big city with python crawl in lianjia
- debug of diminant color coding with wrong function memset
- tensorflow调试之二:ValueError: c of shape (150, 1) not acceptable as a color sequence for x with size 15
- Blending of images, raster operations and basic color adjustments with GDI+
- Ch8.6: write a method to paint-fill a closed ring with the same color
- LeetCode - 30. Substring with Concatenation of All Words
- colorWithAlphaComponent
- android问题 This version of android studio is incompatible with the gradle version used.
- The Prisoner of Zenda——6、An adventure with a teatable
- imageNamed和imageWithContentsOfFile
- Android中自定义常用的三个对象解析(Paint,Color,Canvas)
- The House Of Santa Claus_dfs_2018_2_22
- 严重: A web application created a ThreadLocal with key of type [null] (value [com.sun.faces.config.Con
- imageNamed 与 imageWithContentsOfFile的区别
- 错误argument of type "char *" is incompatible with parameter of type "LPCWSTR"的解决方法
- Error creating bean with name 'sysUserController': Injection of resource dependencies failed; nested