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

阅后即焚 店面

🔗
zpppore 2021-4-7 05:53:31 | 只看该作者
全局:
本帖最后由 zpppore 于 2021-4-7 05:56 编辑

答案唯一的话可以用下面代码
  1.     vector<vector<int>> intervals={{0,3}, {1,3}, {2,4}};
  2.     sort(intervals.begin(), intervals.end(), [&](const vector<int>& i1, const vector<int>& i2){
  3.         return i1[0]<i2[0];
  4.     });
  5.     vector<int> ret={-1,-1};
  6.     int max_overlap = 0;
  7.     auto compare = [](vector<int>& a, vector<int>& b){return a[1]>b[1];};
  8.     priority_queue<vector<int>, vector<vector<int>>, decltype(compare)> pq(compare);
  9.     for (const auto& i: intervals) {
  10.         while(!pq.empty() && pq.top()[1]<i[0])
  11.             pq.pop();
  12.         pq.push(i);
  13.         if (max_overlap<pq.size()) {
  14.             max_overlap = pq.size();
  15.             ret = {i[0], pq.top()[1]};
  16.         }
  17.     }
  18.     return ret;
复制代码

回复

使用道具 举报

🔗
ggtnht77 2021-4-7 15:18:33 | 只看该作者
全局:
dwt585 发表于 2021-4-7 05:17
meeting room套了个马甲

楼主,能仔细说说套了个什么马甲嘛。。。我想了半天都没想出来,求指教啊!
回复

使用道具 举报

🔗
noi10 2021-4-8 12:22:43 | 只看该作者
全局:
请问有没有最大频率区间为多个的情况? 要全输出吗?已加米
回复

使用道具 举报

全局:
noi10 发表于 2021-04-07 21:22:43
请问有没有最大频率区间为多个的情况? 要全输出吗?已加米
对,follow up 输出 最大的range
回复

使用道具 举报

🔗
kevinguo09 2021-4-9 06:37:18 | 只看该作者
全局:
楼主竟然狗屎运过了
回复

使用道具 举报

🔗
noi10 2021-4-9 10:19:44 | 只看该作者
全局:
kevinguo09 发表于 2021-4-9 06:37
楼主竟然狗屎运过了

楼主你又不匿名了
回复

使用道具 举报

🔗
missberry 2021-4-18 13:12:19 | 只看该作者
全局:
本帖最后由 missberry 于 2021-4-18 13:19 编辑

Hi 我想了一个solution,不用pq 时间是linear的, 麻烦大家帮忙看一下对不对 ~ 谢谢!

[/i]
  1. public int[] findInterval(int[][] arr){
  2.         int maxEnd = 0;
  3.         for (int[] a: arr){
  4.             maxEnd = Math.max(a[1], maxEnd);
  5.         }
  6.         // use this 2 dimension array to present how many times a time serves as the start of an interval, and the end of an interval
  7.         // for example store[4] = {3, 0} means 4 is start point of 3 interval and end point of 0 interval in the example
  8.         int[][] store = new int[maxEnd][2];
  9.         for (int[] a: arr){
  10.             store[a[0]][0] ++;
  11.             store[a[1]][1] ++;
  12.         }
  13.         
  14.         int maxFrequency = 0, start = 0, end =0;
  15.         boolean toFindEnd = false; // Indicate if we should start to find the end
  16.         for(int i=0; i<maxEnd; i++) {
  17.             if(store[i][0]> maxFrequency) { // i must be the answer for the start
  18.                 maxFrequency = store[i][0];
  19.                 start = i;
  20.                 toFindEnd = true; // Enable to start search for the endpoint, and only update once.
  21.             } else {
  22.                 if(toFindEnd && store[i][1] > 0 && i>start)
  23.                     end = i;
  24.                     toFindEnd = false;
  25.             }
  26.         }
  27.         return new int[] {start, end};
  28. }
复制代码
[i]

回复

使用道具 举报

🔗
YDL-ideal 2021-4-28 08:39:33 | 只看该作者
全局:
楼主几年经验
回复

使用道具 举报

🔗
avalon516 2021-5-27 15:35:33 | 只看该作者
全局:
请问这个这个频率最大区间不一定是给的interval的区间对吧, 比如你的例子里,如果把input的[4,5]换成[0,5], 最后的结果还是[4,5] 对么?
回复

使用道具 举报

🔗
liusiyu 2021-6-2 09:59:20 | 只看该作者
全局:
回复

使用道具 举报

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

本版积分规则

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