-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMergeIntervals.java
More file actions
147 lines (92 loc) · 3 KB
/
MergeIntervals.java
File metadata and controls
147 lines (92 loc) · 3 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
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
/*
Merge Intervals
Problem Description
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Problem Constraints
1 <= |intervals| <= 105
Input Format
First argument is the vector of intervals
second argument is the new interval to be merged
Output Format
Return the vector of intervals after merging
Example Input
Input 1:
Given intervals [1, 3], [6, 9] insert and merge [2, 5] .
Input 2:
Given intervals [1, 3], [6, 9] insert and merge [2, 6] .
Example Output
Output 1:
[ [1, 5], [6, 9] ]
Output 2:
[ [1, 9] ]
Example Explanation
Explanation 1:
(2,6) does not completely merge the given intervals
Explanation 2:
(2,6) completely merges the given intervals
*/
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
/*
Solution Approach
Have you covered the following corner cases :
1) Size of interval array as 0.
2) newInterval being an interval preceding all intervals in the array.
Given interval (3,6),(8,10), insert and merge (1,2)
3) newInterval being an interval succeeding all intervals in the array.
Given interval (1,2), (3,6), insert and merge (8,10)
4) newInterval not overlapping with any interval and falling in between 2 intervals in the array.
Given interval (1,2), (8,10) insert and merge (3,6)
5) newInterval covering all given intervals.
Given interval (3, 5), (7, 9) insert and merge (1, 10)
*/
public class Solution {
public ArrayList<Interval> insert(ArrayList<Interval> list, Interval elem) {
elem = new Interval(Integer.min(elem.start, elem.end), Integer.max(elem.start, elem.end));
Deque<Interval> dq = new LinkedList<>();
boolean isAdded = false;
for(int i=0;i<list.size();i++){
if(!isAdded){
if(list.get(i).start>=elem.start){
addElemStack(dq, elem);
addElemStack(dq, list.get(i));
}
else{
dq.addLast(list.get(i));
}
isAdded = true;
}
else{
addElemStack(dq, list.get(i));
}
}
if(!isAdded){
if(dq.size()>0)
addElemStack(dq, elem);
else
dq.add(elem);
}
ArrayList<Interval> res = new ArrayList<>();
while(dq.size()>0){
res.add(dq.removeFirst());
}
return res;
}
public void addElemStack(Deque<Interval> dq, Interval elem){
if(dq.size()>0 && dq.peekLast().end>=elem.start){
Interval temp = dq.removeLast();
dq.addLast(new Interval(Integer.min(temp.start, elem.start), Integer.max(temp.end, elem.end)));
}
else{
dq.addLast(elem);
}
}
}