-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertInterval_mid.cc
152 lines (130 loc) · 4.08 KB
/
insertInterval_mid.cc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<vector>
#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) {}
};
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
int n = intervals.size();
if (!n) return vector<Interval>(1, newInterval);
int i = 0;
while (i < n && intervals[i].start < newInterval.start) i++;
if (!i)
intervals.insert(intervals.begin(), newInterval);
else if (i == n)
intervals.push_back(newInterval);
else
intervals.insert(intervals.begin() + i, newInterval);
return merge(intervals);
}
vector<Interval> merge(vector<Interval> &intervals) {
int n = intervals.size();
if (!n) return vector<Interval>();
vector<Interval> ret(1, intervals[0]);
for (int i = 1; i < n; ++i) {
int k = ret.size() - 1;
int end = ret[k].end;
if (end < intervals[i].start) {
ret.push_back(intervals[i]);
continue;
} else if (end >= intervals[i].end) {
continue;
} else {
ret[k].end = intervals[i].end;
}
}
return ret;
}
//-----------------------------------------------------------//
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> ret;
int first = -1;
int last = -1;
bool inserted = false;
intervals.push_back(newInterval);// if the interval is the last interval, if it not,this can ensure that we won't access some space that is null
for (int i = 0; i < intervals.size(); ++i) {
if (i == (intervals.size() - 1) && inserted) break;// avoid insert newInterval twice
Interval tmp = intervals[i];
if (!inserted && newInterval.start < tmp.start) {
inserted = true;
tmp = newInterval;
i--;
}
if (first == -1) {
first = tmp.start;
last = tmp.end;
} else if (tmp.start <= last) { // already ensure that tmp.start > first because line 24 in last round;
last = max(tmp.end, last);
} else {
Interval a(first, last);
ret.push_back(a);
first = tmp.start;
last = tmp.end;
}
}
Interval a(first, last);
ret.push_back(a);
return ret;
}
};
//----------------------------------------//
struct myclass {
bool operator() (const Interval i,const Interval j) { return (i.start < j.start);}
} myOp;
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
int n = intervals.size();
if (!n) return intervals;
sort(intervals.begin(), intervals.end(), myOp);
vector<Interval> ret(1, intervals[0]);
for (int i = 1; i < n; i++) {
Interval &tmp = ret.back();
if (tmp.end > intervals[i].end) continue;
if (tmp.end >= intervals[i].start) tmp.end = intervals[i].end;
else ret.push_back(intervals[i]);
}
return ret;
}
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
intervals.push_back(newInterval);
return merge(intervals);
}
};
int main(int argc, char **argv) {
Solution sol;
vector<Interval> intervals;
Interval newInt(0, 9);
Interval a1(1, 5);
Interval a2(6, 8);
intervals.push_back(a1);
intervals.push_back(a2);
vector<Interval> ret = sol.insert(intervals, newInt);
for (int i = 0; i < ret.size(); ++i) {
cout << ret[i].start << " " << ret[i].end << endl;
}
cout << "---------------------------------" << endl;
intervals.clear();
Interval b1(1, 2);
Interval b2(3, 5);
Interval b3(6, 7);
Interval b4(8, 10);
Interval b5(12, 16);
intervals.push_back(b1);
intervals.push_back(b2);
intervals.push_back(b3);
intervals.push_back(b4);
intervals.push_back(b5);
Interval newInt1(4, 9);
ret = sol.insert(intervals, newInt1);
for (int i = 0; i < ret.size(); ++i) {
cout << ret[i].start << " " << ret[i].end << endl;
}
return 1;
}