📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
123
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

Facebook Additional Phone Interview

🔗
adiggo 2014-11-19 09:30:05 | 只看该作者
全局:
sweeney1130 发表于 2014-11-19 03:53
先排序,然后再扫一遍,感觉代码写的很冗余。

看着有点头疼。。。
回复

使用道具 举报

🔗
adiggo 2014-11-19 09:30:17 | 只看该作者
全局:
sweeney1130 发表于 2014-11-19 03:53
先排序,然后再扫一遍,感觉代码写的很冗余。

看着有点头疼。。。
回复

使用道具 举报

🔗
中庸人90 2014-11-19 09:45:59 | 只看该作者
全局:
这道题感觉不用排序的啊
  1. public void printIntervals(Employee[] employees){
  2.                 // the whole range from l ~ r
  3.                 int l = employees[0].startTime;
  4.                 int r = employees[0].endTime;
  5.                 for(Employee e : employees){
  6.                         l = Math.min(l, e.startTime);
  7.                         r = Math.max(r, e.endTime);
  8.                 }

  9.                 int[] timeSlots = new int[r-l+1];
  10.                 boolean[] delimiters = new boolean[r-l+1];
  11.                 for(Employee e : employees){
  12.                         timeSlots[e.startTime-l]++;
  13.                         timeSlots[e.endTime-l]--;
  14.                         delimiters[e.startTime-l] = true;
  15.                         delimiters[e.endTime-l] = true;
  16.                 }

  17.                 int count = timeSlots[0];
  18.                 int preStart = 0;
  19.                 for(int i = 1; i < timeSlots.length; i++){
  20.                         if(delimiters[i] == false) continue;
  21.                         System.out.println((preStart+l) + "-" + (i+l) + ": " + count);
  22.                         count += timeSlots[i];
  23.                         preStart = i;
  24.                 }
  25.         }
复制代码
欢迎指正~
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-VVM0T  2014-11-19 10:14:28
中庸人90 发表于 2014-11-19 09:45
这道题感觉不用排序的啊欢迎指正~

我最初解 space不达标
回复

使用道具 举报

🔗
sweeney1130 2014-11-19 16:17:46 | 只看该作者
全局:
中庸人90 发表于 2014-11-19 09:45
这道题感觉不用排序的啊欢迎指正~

我感觉这个算法问题就在于时间复杂度是O(T+n)的,T是最长和最短时间的间隔吧,所以万一T特别大,那可能效率就很慢了。
回复

使用道具 举报

🔗
sweeney1130 2014-11-19 16:17:54 | 只看该作者
全局:
中庸人90 发表于 2014-11-19 09:45
这道题感觉不用排序的啊欢迎指正~

我感觉这个算法问题就在于时间复杂度是O(T+n)的,T是最长和最短时间的间隔吧,所以万一T特别大,那可能效率就很慢了。
回复

使用道具 举报

🔗
中庸人90 2014-11-20 04:37:10 | 只看该作者
全局:
sweeney1130 发表于 2014-11-19 16:17
我感觉这个算法问题就在于时间复杂度是O(T+n)的,T是最长和最短时间的间隔吧,所以万一T特别大,那可能效 ...

对,排序的话可以离散化,会省空间
回复

使用道具 举报

🔗
jarfield 2015-10-7 22:53:16 | 只看该作者
全局:
  1. class TimeNode {
  2. public:
  3.     int time;
  4.     bool start;
  5.    
  6. public:
  7.     TimeNode(int time, bool start) : time(time), start(start) {}
  8.    
  9.     bool operator < (const TimeNode &t) const {
  10.         return this->time < t.time;
  11.     }
  12. };

  13. class Solution {
  14. public:
  15.     vector<string> statistics(const vector<Employee> &employees) {
  16.         // base case
  17.         vector<string> result;
  18.         if (employees.empty()) return result;
  19.         
  20.         // sort
  21.         vector<TimeNode> times;
  22.         for (const auto employee : employees) {
  23.             times.push_back(TimeNode(employee.startTime, true));
  24.             times.push_back(TimeNode(employee.endTime, false));
  25.         }
  26.         sort(times.begin(), times.end());
  27.         
  28.         // scan
  29.         int i = 1, count = 1, begin = 0;
  30.         while (i < times.size()) {
  31.             if (times[i].time != times[i - 1].time) {
  32.                 result.push_back(make_statistics(times[begin].time, times[i].time, count));
  33.                 begin = i;
  34.             }

  35.             count += times[i].start? 1: -1;
  36.             i++;
  37.         }
  38.         
  39.         return result;
  40.     }
  41.    
  42.     string make_statistics(int start, int end, int count) {
  43.         return to_string(start) + "->" + to_string(end) + ": " + to_string(count);
  44.     }   
  45. };

  46. int main(int argc, const char * argv[]) {
  47.     vector<Employee> employees;
  48.     employees.push_back(Employee(4, 2004, 2015));
  49.     employees.push_back(Employee(7, 2011, 2015));
  50.     employees.push_back(Employee(9, 2004, 2009));
  51.     employees.push_back(Employee(11, 2007, 2008));
  52.     employees.push_back(Employee(12, 2008, 2014));
  53.     employees.push_back(Employee(15, 2013, 2014));

  54.     for (const auto str : Solution().statistics(employees)) {
  55.         cout << str << endl;
  56.     }
  57.    
  58.     return 0;
  59. }
复制代码
回复

使用道具 举报

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

本版积分规则

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