-
Notifications
You must be signed in to change notification settings - Fork 22
/
FindNonOverlappingIntervals
213 lines (187 loc) · 9.33 KB
/
FindNonOverlappingIntervals
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
************************ I. Using Comparable(natural ordering) interface*************************************
/*NOTE: This question is implemented both using Comparable(natural ordering) and Comparator(custom defined ordering) interfaces
*
* Question: Find the maximum number of non-intersecting events in a calendar.
* Source: http://www.careercup.com/question?id=5666830939062272
*
* Algorithm: 1. Sort the array of intervals on start time
* 2. Push first interval in stack or arrayList or any other array (NOTE: we just need a storage data structure)
* 3. Check if the next interval overlap with the first interval
* 3.a. If NO, then add the element into the data structure
* 3.b. If YES, the Intervals overlap. Check which interval ends first, that interval should remain in the stack
*
* VERY IMP Question: How to check if an interval overlaps with another interval ?
* VERY IMP Answer: If there are two intervals a and b.
* Then if (a.endTime < b.startTime) OR (a.startTime > b.endTime) then the intervals DONOT OVERLAP, otherwise they do overlap.
* Thus, we only need to check the startTime of one with endTime of the other OR vice-versa to check for OVERLAP condition
*/
package findNonOverlappingIntervals;
import java.util.Arrays;
import java.util.Stack;
public class UsingSortingOnStartDateUsingComparable {
public static void main(String[] args) {
IntervalOfTime[] intervals = new IntervalOfTime[]{new IntervalOfTime(1,4),
new IntervalOfTime(2,3),
new IntervalOfTime(5,8),
new IntervalOfTime(4,5),
new IntervalOfTime(3,7),
new IntervalOfTime(6,10),
new IntervalOfTime(7,8),
new IntervalOfTime(9,12)};
System.out.println("The maximum number of non-overlapping intervals USING SORTING: "+usingSorting(intervals));
}
private static int usingSorting(IntervalOfTime[] intervals) {
Stack<IntervalOfTime> stack = new Stack<IntervalOfTime>();
Arrays.sort(intervals); // Natural Ordering of Arrays.sort() method, URL: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])
// push the first interval in the stack
stack.push(intervals[0]);
// start with the second interval and start comparing with the last interval in the stack
for(int i=1;i<intervals.length;i++){
IntervalOfTime last = stack.peek();
IntervalOfTime current = intervals[i];
// check for overlap
if(last.endTime <= current.startTime) // does not overlap, push into stack.
/*
* NOTE: Since we have sorted according to start times hence we DO NOT need to check if (last.startTime >= current.endTime)
*/
stack.push(current);
else // Intervals overlap. Check which interval ends first, that interval should remain in the stack
if(current.endTime < last.endTime){
stack.pop();
stack.push(current);
}
}
return stack.size(); // since the stack contains all non-overlapping intervals
}
/*
* Analysis:
* Time Complexity = O(nlgn) where n is the number of intervals
* Space Complexity = O(n)
*
*/
}
/*
* If natural ordering is specified means we have to use (java.lang package) i.e. Comparable<Object> interface
* If Comparator is specified then we have to use (java.util package) i.e. Comparator<Object> interface
* VERY VERY VERY IMPORTANT: For deep understanding of custom defined Comparator, please refer this URL: http://www.programcreek.com/2013/11/arrays-sort-comparator/
*
*
* VERY IMP: natural ordering = java.lang
* customer defined ordering = java.util
*
* NOTE: In case of sort method of arrays, we have both variations of sort method.
*
* 1. Arrays.sort(array) with natural ordering URL: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])
* 2. Arrays.sort(array, Comparator Object) without natural ordering URL: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)
* VERY VERY VERY IMPORTANT: For deep understanding of custom defined Comparator, please refer this URL: http://www.programcreek.com/2013/11/arrays-sort-comparator/
*/
class IntervalOfTime implements Comparable<IntervalOfTime>{
int startTime;
int endTime;
public IntervalOfTime(int startTime, int endTime){
this.startTime = startTime;
this.endTime = endTime;
}
/*
* VERY VERY VERY IMP NOTE: If we are using Comparable, then we DONOT need a separate class, since we are NOT PASSING any instance to Arrays.sort() method
*/
@Override
public int compareTo(IntervalOfTime o) {
return this.startTime - o.startTime;
}
}
**************************** II. Using Comparator(custom defined ordering) interface ****************************
/*NOTE: This question is implemented both using Comparable(natural ordering) and Comparator(custom defined ordering) interfaces
*
* Question: Find the maximum number of non-intersecting events in a calendar.
* Source: http://www.careercup.com/question?id=5666830939062272
*
* Algorithm: 1. Sort the array of intervals on start time
* 2. Push first interval in stack or arrayList or any other array (NOTE: we just need a storage data structure)
* 3. Check if the next interval overlap with the first interval
* 3.a. If NO, then add the element into the data structure
* 3.b. If YES, the Intervals overlap. Check which interval ends first, that interval should remain in the stack
*
* VERY IMP Question: How to check if an interval overlaps with another interval ?
* VERY IMP Answer: If there are two intervals a and b.
* Then if (a.endTime < b.startTime) OR (a.startTime > b.endTime) then the intervals DONOT OVERLAP, otherwise they do overlap.
* Thus, we only need to check the startTime of one with endTime of the other OR vice-versa to check for OVERLAP condition
*/
package findNonOverlappingIntervals;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Stack;
public class UsingSortingOnStartDateUsingComparator {
public static void main(String[] args) {
Interval[] intervals = new Interval[]{new Interval(1,4),
new Interval(5,8),
new Interval(2,3),
new Interval(4,5),
new Interval(3,7),
new Interval(6,10),
new Interval(7,8),
new Interval(9,12)};
System.out.println("The maximum number of non-overlapping intervals USING SORTING: "+usingSorting(intervals));
}
private static int usingSorting(Interval[] intervals) {
Stack<Interval> stack = new Stack<Interval>();
Arrays.sort(intervals, new IntervalComparator()); // Custom Ordering of Arrays.sort() method, URL: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)
// push the first interval in the stack
stack.push(intervals[0]);
// start with the second interval and start comparing with the last interval in the stack
for(int i=1;i<intervals.length;i++){
Interval last = stack.peek();
Interval current = intervals[i];
// check for overlap
if(last.endTime <= current.startTime) // does not overlap, push into stack.
/*
* NOTE: Since we have sorted according to start times hence we DO NOT need to check if (last.startTime >= current.endTime)
*/
stack.push(current);
else // Intervals overlap. Check which interval ends first, that interval should remain in the stack
if(current.endTime < last.endTime){
stack.pop();
stack.push(current);
}
}
return stack.size(); // since the stack contains all non-overlapping intervals
}
/*
* Analysis:
* Time Complexity = O(nlgn) where n is the number of intervals
* Space Complexity = O(n)
*
*/
}
/*
* If natural ordering is specified means we have to use (java.lang package) i.e. Comparable<Object> interface
* If Comparator is specified then we have to use (java.util package) i.e. Comparator<Object> interface
* VERY VERY VERY IMPORTANT: For deep understanding of custom defined Comparator, please refer this URL: http://www.programcreek.com/2013/11/arrays-sort-comparator/
*
*
* VERY IMP: natural ordering = java.lang
* customer defined ordering = java.util
*
* NOTE: In case of sort method of arrays, we have both variations of sort method.
*
* 1. Arrays.sort(array) with natural ordering URL: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])
* 2. Arrays.sort(array, Comparator Object) without natural ordering URL: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)
* VERY VERY VERY IMPORTANT: For deep understanding of custom defined Comparator, please refer this URL: http://www.programcreek.com/2013/11/arrays-sort-comparator/
*/
class Interval{
int startTime;
int endTime;
public Interval(int startTime, int endTime){
this.startTime = startTime;
this.endTime = endTime;
}
}
class IntervalComparator implements Comparator<Interval>{
/*
* VERY VERY VERY IMP NOTE: If we are using Comparator, then we need a separate class, since the instance of this class is passed to Arrays.sort() method
*/
@Override
public int compare(Interval a, Interval b) {
return a.startTime-b.startTime;
}
}