📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3285| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

Google : Paint houses in the cheapest way

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
There are a row of houses, each house can be painted with three colors red,
blue and 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.
You have to paint the houses with minimum cost. How would you do it?
Note: Painting house-1 with red costs different from painting house-2 with red.
The costs are different for each house and each color.

看来Google喜欢DP的题

上一篇:Facebook : Print subset
下一篇:问一个关于快速排序的问题
🔗
 楼主| wwwyhx 2011-8-27 01:46:17 | 只看该作者
全局:
没测试这个

  1. const int N = 20; //house number

  2. int GetLeastCost(int A[3][N])
  3. {
  4.         int DP[3][N];
  5.         DP[0][0] = A[0][0];
  6.         DP[1][0] = A[1][0];
  7.         DP[2][0] = A[2][0];

  8.         for (int j = 1; j < N; j++)
  9.         {
  10.                 DP[0][j] = A[0][j] + min(A[1][j], A[2][j]);
  11.                 DP[1][j] = A[1][j] + min(A[0][j], A[2][j]);
  12.                 DP[2][j] = A[2][j] + min(A[1][j], A[0][j]);
  13.         }

  14.         return min(min(DP[0][N-1], DP[1][N-1]), DP[2][N-1]);
  15. }
复制代码
回复

使用道具 举报

🔗
modifiedname 2011-8-27 01:54:35 | 只看该作者
全局:
这个能保证local minimum, 如何保证也是global minimum?
回复

使用道具 举报

🔗
lambda2fei 2012-9-3 16:33:29 | 只看该作者
全局:
一个dp,f(i,j)表示前i个house都paint了且第i个house paint成color_j的最小cost。
回复

使用道具 举报

🔗
CStick75 2012-9-6 22:04:16 | 只看该作者
全局:
DP O(N)  
f[i][j] = min{f[i-1][k] + cost[j]:k!=j}
边界 f[1] = {cost[0],cost[1],cost[2]}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表