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

脸熟体验不是很好的电面

全局:
lorii 发表于 2021-02-19 23:54:47
非常感谢这个帖子和这个回复! 面到了类似的变种题 用的二分的思路做了 今天通知过了 希望楼主和层主都好运!
恭喜! 能帮到地里的各位就好
回复

使用道具 举报

🔗
aaaaal 2021-2-27 10:47:04 | 只看该作者
本楼:
全局:
多谢分享
回复

使用道具 举报

🔗
whiteboard 2021-3-12 08:13:06 | 只看该作者
全局:
follow up,  一个list比另一个短特别多的情况:  O(MlogN),M是短list的长度,N是长list的长度

  1. class Solution {
  2. public:
  3.     vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
  4.         int n_first = firstList.size();
  5.         int n_second = secondList.size();
  6.             
  7.         if (n_first < n_second) {
  8.             for (auto &interval : firstList)
  9.                 binary_search(interval, secondList);
  10.         } else {
  11.             for (auto &interval : secondList)
  12.                 binary_search(interval, firstList);
  13.         }   
  14.             
  15.         return result;
  16.     }   
  17.         
  18. private:
  19.     vector<vector<int>> result;

  20.     void binary_search(vector<int> &interval, vector<vector<int>> &longList) {
  21.         auto comp = [](const vector<int> &interval1, const vector<int> &interval2) {
  22.             return interval1[1] < interval2[1];
  23.         };  
  24.             
  25.         vector<int> itv = {interval[0], interval[0]};
  26.         auto it_left = lower_bound(longList.begin(), longList.end(), itv, comp);
  27.         if (it_left == longList.end())
  28.             return;
  29.             
  30.         itv = {interval[1], interval[1]};
  31.         auto it_right = lower_bound(longList.begin(), longList.end(), itv, comp);
  32.         if (it_right != longList.end())
  33.             it_right++;
  34.       
  35.         for (; it_left < it_right; it_left++) {
  36.             vector<int> overlap = {max(interval[0], (*it_left)[0]),
  37.                              min(interval[1], (*it_left)[1])};
  38.                
  39.             if (overlap[0] <= overlap[1])
  40.                 result.push_back(overlap);
  41.         }   
  42.     }   
  43. };
复制代码
回复

使用道具 举报

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

本版积分规则

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