-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert-interval.cpp
77 lines (75 loc) · 1.8 KB
/
insert-interval.cpp
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
/**
* Author: Vindru
* Problem: (https://leetcode.com/problems/insert-interval/submissions/)
*/
class Solution
{
public:
vector<vector<int> > insert(vector<vector<int> > &intervals, vector<int> &newInterval)
{
bool isOneReached = false, istwoReached = false, rp = false;
vector<vector<int> > res;
vector<int> res1;
if (intervals.size() == 0)
{
res.push_back(newInterval);
return res;
}
for (int i = 0; i < intervals.size(); i++)
{
for (int k = 0; k < res1.size(); k++)
cout << res1[k] << " ";
if (!isOneReached)
{
if (newInterval[0] <= intervals[i][1] && newInterval[0] >= intervals[i][0])
{
res1.push_back(intervals[i][0]);
isOneReached = true;
}
else if (newInterval[0] < intervals[i][0])
{
res1.push_back(newInterval[0]);
isOneReached = true;
}
}
if (!istwoReached)
{
if (newInterval[1] <= intervals[i][1] && newInterval[1] >= intervals[i][0])
{
res1.push_back(intervals[i][1]);
istwoReached = true;
}
else if (newInterval[1] < intervals[i][0])
{
i--;
res1.push_back(newInterval[1]);
istwoReached = true;
}
}
if (!isOneReached && !istwoReached)
{
res.push_back(intervals[i]);
}
else if (isOneReached && istwoReached && rp == false)
{
res.push_back(res1);
rp = true;
}
else if (rp)
{
res.push_back(intervals[i]);
}
}
if (!isOneReached && !istwoReached && rp == false)
{
res.push_back(newInterval);
}
else if (res1.size() == 1)
{
res1.push_back(newInterval[1]);
res.push_back(res1);
rp = true;
}
return res;
}
};