博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CareerCup Find all the conflicting appointments from a given list of n appointments.
阅读量:4184 次
发布时间:2019-05-26

本文共 2546 字,大约阅读时间需要 8 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
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
> conflicts(len + 1); vector
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;}

转载地址:http://twboi.baihongyu.com/

你可能感兴趣的文章
MyEclipse中WEB项目加载mysql驱动方法
查看>>
常见编写JAVA报错总结
查看>>
org.gjt.mm.mysql.Driver和com.mysql.jdbc.Driver的区别
查看>>
UTF-8和GBK有什么区别
查看>>
增加MyEclipse分配内存的方法
查看>>
头痛与早餐
查看>>
[转]在ASP.NET 2.0中操作数据::创建一个数据访问层
查看>>
Linux命令之chmod详解
查看>>
【java小程序实战】小程序注销功能实现
查看>>
leetcode Unique Paths II
查看>>
几个大数据的问题
查看>>
CareerCup Pots of gold game:看谁拿的钱多
查看>>
CarreerCup Sort Height
查看>>
CareerCup Sort an array in a special way
查看>>
CareerCup Find lexicographic minimum in a circular array 循环数组字典序
查看>>
CareerCup Cost of cutting wood
查看>>
Find the number of subsets such that the sum of numbers in the subset is a prime number
查看>>
CareerCup Binary Tree the Maximum of 人
查看>>
CareerCup Divide n cakes to k different people
查看>>
CareerCup Randomly return a node in a binary tree
查看>>