CareerCup Find all the conflicting appointments from a given list of n appointments.

265 篇文章 1 订阅
86 篇文章 0 订阅

You are given 'n' appointments. Each appointment contains startime and endtime. You have to return all conflicting appointments efficiently starttime and endtime can range from a few min to few years.

----------------------------------------------------------------------------------------------------------

Three problems:

Insert Interval: O(n). You can use binary search to locate the insert position, However, the complexity of bad case is still O(n).

Merge Interval: O(nlogn)  The complexity of sort.

List conflicting intervals needs O(nlogn) + O(nlogn)   sort + search



Approach:

Suppose there are 5 events:
E1 => 13 : 15
E2 => 18 : 20
E3 => 11 : 24
E4 => 19 : 27
E5 =>  4  : 12

Now sort the events with start time:


E5: 4 : 12
E3: 11 : 24
E1: 13 : 15
E2: 18 : 20
E4: 19: 27


Now take the end time of first Event (E5) ie 12 and check in the start time the first event whose start time is greater than 12, in the above example E1 - with start time 13 is the event.
Now we can safely say that all the events less than E1 and greater than E5 are conflicting with E5 - which is E3 in our case.
With the same above logic, E3 is conflicting with E1, E2 and E4.
Time complexity = O(nlogn) for sorting the events on start time + O(nlogn) for searching a all the conflicting events for a given event Ei (1 <= i <= n).
So total time complexity: O(nlogn)


Output of the below code:

Events sorted with start time:
5: 4:12
3: 11:24
1: 13:15
2: 18:20
4: 19:27
Conflicts are: 
1 <> 3 
2 <> 3 4 
3 <> 5 1 2 4 
4 <> 3 2 
5 <> 3 

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <memory.h>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
class Interval {
public:
  int s,e,index;
  Interval(int ss = 0, int ee = 0, int ii = 0):s(ss),e(ee),index(ii){  
  }
};
class cmp {
public:
  bool operator()(Interval& x, Interval& y) {
    if (x.s == y.s)
      return x.index < y.index;
    else
    return x.s < y.s;
  }
};
int main() {
  Interval intervals[5] = {Interval(4,12,5), Interval(11,24,3), Interval(13,15,1), Interval(18,20,2), Interval(19,27,4)};
  int i, j, s, e, len = sizeof(intervals) / sizeof(Interval);
  vector<vector<int>> conflicts(len + 1);
  vector<Interval> vec(intervals, intervals + len);
  sort(vec.begin(), vec.end(), cmp());
  for (i = 0; i < len; ++i) {
    s = i + 1, e = len - 1;

    int res = i;
    while (s <= e) {
      int mid = (s + e) / 2;
      if (vec[mid].s > vec[i].e)
        e = mid - 1;
      else {
        res = mid;
        s = mid + 1;
      }
    }
    for (j = i + 1; j <= res; ++j) {
      conflicts[intervals[i].index].push_back(intervals[j].index);
      conflicts[intervals[j].index].push_back(intervals[i].index);
    }
  }
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值