-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57. Insert Interval.cpp
More file actions
59 lines (57 loc) · 1.65 KB
/
57. Insert Interval.cpp
File metadata and controls
59 lines (57 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
int binarysearch(vector<int>& nums, int target)
{
if (nums[0] > target)
return -1;
else if (nums[nums.size() - 1] < target)
return nums.size();
else
{
int bi = 0, ei = nums.size() - 1;
while (bi < ei)
{
int mid = bi + (ei - bi) / 2;
if (nums[mid] > target)
ei = mid - 1;
else
bi = mid + 1;
}
return bi;
}
}
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval)
{
int sz = intervals.size();
vector<Interval> res;
for(int i=0; i<sz; i++)
{
if(intervals[i].end < newInterval.start)
{
res.push_back( intervals[i]);
}
else if( intervals[i].start > newInterval.end)
{
res.push_back(newInterval);
res.insert(res.end(), intervals.begin()+i, intervals.end());
return res;
}
else
{
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
}
}
res.push_back(newInterval);
return res;
}
};