楼主: Onedayw
跳转到指定楼层
上一主题 下一主题
收起左侧

snapchat店面跪经 求指点

🔗
Ridingstar01 2016-10-10 02:40:13 | 只看该作者
全局:
我觉得有可能是代码风格比如变量名命名,用的i, i1, i2什么的,你出的那个bug是reference的问题,但是追究到底是代码风格不规范造成的。
回复

使用道具 举报

🔗
bearcat001 2016-10-11 11:46:09 | 只看该作者
全局:
明天面试,多谢楼主面经,写了一份看起来还算简单的代码,用两个优先队列。一个用来存正在进行的meeting和这个meeting使用的roomId,一个用来存可用的room。每次新来一个meeting的时候,把结束的meeting释放出来,并且把room也释放。
  1. import java.util.*;
  2. class Untitled {
  3.     public static void getAllRooms(int[][] rooms) {
  4.         if (rooms == null || rooms.length == 0) {
  5.             return;
  6.         }

  7.         Arrays.sort(rooms, new Comparator<int[]>(){
  8.             public int compare(int[] a, int[] b) {
  9.                 if (a[0] == b[0]) {
  10.                     return a[1] - b[1];
  11.                 } else {
  12.                     return a[0] - b[0];
  13.                 }
  14.             }
  15.         });
  16.         
  17.         PriorityQueue<int[]> running = new PriorityQueue<>(10, new Comparator<int[]>() {
  18.             public int compare(int[] a, int[] b) {
  19.                 if (a[0] == b[0]) {
  20.                     return a[1] - b[1];
  21.                 } else {
  22.                     return a[0] - b[0];
  23.                 }
  24.             }
  25.         });
  26.         PriorityQueue<Integer> emptyRoom = new PriorityQueue<>();
  27.         List<List<int[]>> result = new ArrayList<>();

  28.         for (int i = 0; i < rooms.length; i++) {
  29.             int[] current = rooms[i];
  30.             // pop finished meeting first
  31.             while(running.size() != 0 && current[0] >= running.peek()[0]) {
  32.                 int[] run = running.remove();
  33.                 emptyRoom.add(run[1]);
  34.             }
  35.             // get the room id
  36.             int id = 0;
  37.             if (emptyRoom.size() != 0) {
  38.                 id = emptyRoom.remove();
  39.             } else {
  40.                 id = result.size();
  41.                 result.add(new ArrayList<>());
  42.             }
  43.             // add current to room
  44.             result.get(id).add(current);
  45.             // run meeting
  46.             running.add(new int[]{current[1], id});
  47.         }

  48.         for (int i = 0; i < result.size(); i++) {
  49.             System.out.print("Room " + (i + 1) + ": ");
  50.             for (int[] meeting : result.get(i)) {
  51.                 System.out.print("[" + meeting[0] + "," + meeting[1] + "],");
  52.             }
  53.             System.out.println("");
  54.         }
  55.     }

  56.     public static void main(String[] args) {
  57.         getAllRooms(new int[][]{{3,6}, {6,9}, {5,7}});
  58.     }
  59. }
复制代码
回复

使用道具 举报

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

本版积分规则

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