123
返回列表 发新帖
楼主: jwl2006
跳转到指定楼层
上一主题 下一主题
收起左侧

空气床onsite

🔗
laecheng 2019-1-24 13:19:37 | 只看该作者
全局:
感觉空气床DP其实考得不多
回复

使用道具 举报

🔗
laecheng 2019-1-24 13:47:11 | 只看该作者
全局:
楼主能帮我看下这样做对吗?谢谢
  1. class Solution {
  2.     public static void main(String[] args) {
  3.         Solution sol = new Solution();
  4.         List<List<String>> input = new ArrayList<>();
  5.         input.add(Arrays.asList("2-4", "100"));
  6.         input.add(Arrays.asList("4-8", "200"));
  7.         input.add(Arrays.asList("5-7", "300"));
  8.         System.out.println(sol.findMaxProfit(input));
  9.     }
  10.    
  11.     public int findMaxProfit(List<List<String>> input) {
  12.         if (input == null || input.size() == 0) return 0;
  13.         List<Interval> orders = new ArrayList<>();
  14.         for (List<String> list : input) {
  15.             String[] time = list.get(0).split("-");
  16.             int start = Integer.parseInt(time[0]);
  17.             int end = Integer.parseInt(time[1]);
  18.             int profit = Integer.parseInt(list.get(1));
  19.             orders.add(new Interval(start, end, profit));
  20.         }
  21.         Collections.sort(orders, (o1, o2) -> {
  22.             return Integer.compare(o1.start, o2.start);
  23.         });
  24.         // DP => look back all results
  25.         int[] M = new int[orders.size()];
  26.         M[0] = orders.get(0).profit;
  27.         int global = 0;
  28.         for (int i = 1; i < orders.size(); i++) {
  29.             int max = orders.get(i).profit;
  30.             for (int j = 0; j < i; j++) {
  31.                 if (orders.get(j).end <= orders.get(i).start) { // if no overlap
  32.                     max = Math.max(max, M[j] + max);
  33.                 }
  34.             }
  35.             M[i] = max;
  36.             global = Math.max(global, max);
  37.         }
  38.         return global;
  39.     }
  40. }

  41. class Interval {
  42.     int start;
  43.     int end;
  44.     int profit;
  45.     public Interval(int start, int end, int profit) {
  46.         this.start = start;
  47.         this.end = end;
  48.         this.profit = profit;
  49.     }
  50. }
  51.    
复制代码

补充内容 (2019-1-24 14:04):
32应该是max = Math.max(max, M[j] + orders.get(i).profit);

补充内容 (2019-1-24 14:06):
27 global = M[0]
回复

使用道具 举报

🔗
sifangyou1 2019-6-9 14:45:45 | 只看该作者
全局:
请问楼主按照 start 还是 end time 来排序?

“7. coding:  新题,反正我没见过。题目是给你一堆24小时内旅店预定的信息,{{"2-4", "100"}, {"4-8", "200"}, {"5-7", "300"}},要求找出收入最大的on-overlapping的时间段。现场写出来了。”
回复

使用道具 举报

🔗
longvision 2019-11-6 05:09:27 | 只看该作者
全局:
laecheng 发表于 2019-1-24 13:47
楼主能帮我看下这样做对吗?谢谢
[mw_shl_code=java,true]class Solution {
    public static void main ...

请问dp里面的M[n]代表什么?
回复

使用道具 举报

全局:
感谢分享
回复

使用道具 举报

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

本版积分规则

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