-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeInterval1.cpp
More file actions
75 lines (56 loc) · 2 KB
/
mergeInterval1.cpp
File metadata and controls
75 lines (56 loc) · 2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
#include <stack>
#include <iterator>
#include <algorithm>
using namespace std;
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
Interval compareInterval = Interval(std::min(newInterval.start, newInterval.end),
std::max(newInterval.start, newInterval.end));
stack<Interval> s;
s.push(compareInterval);
Interval currentInterval;
for (int i=0; i<intervals.size(); ++i) {
currentInterval = intervals[i];
Interval stackTop = s.top();
if (stackTop.end < currentInterval.start)
{
s.push(currentInterval);
} else {
if (currentInterval.end < stackTop.start) {
s.pop();
s.push(currentInterval);
s.push(compareInterval);
} else {
s.pop();
s.push(Interval(std::min(stackTop.start, currentInterval.start),
std::max(stackTop.end, currentInterval.end)) );
}
}
}
std::vector<Interval> output;
while( !s.empty()){
Interval elem = s.top(); s.pop();
output.push_back(elem);
}
reverse(output.begin(), output.end());
return output;
}
int main() {
vector<Interval> v;
v.push_back(Interval(3,5));
v.push_back(Interval(7,9));
Interval i(1,10);
vector<Interval> output = insert(v,i);
//copy(output.begin(), output.end(), ostream_iterator<Interval>(cout," "))
}