Merge Intervals
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.
Example 1:
Given intervals [1,3],[6,9] insert and merge [2,5] would result in [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] would result in [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
Make sure the returned intervals are also sorted.
My approach:
/**
* 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; }
* }
*/
public class Solution {
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
int mStart = newInterval.start;
int mEnd = newInterval.end;
ArrayList<Interval> ans = new ArrayList<Interval>();
Interval inter = new Interval();
int tmp;
//Check if interval is larger than the elements present in the array
int count = 0;
if( inter.start > inter.end)
{
tmp = inter.start;
inter.start = inter.end;
inter.end = tmp;
}
//Base case when intervals has 0 size
if( intervals.size() == 0 )
{
ans.add(newInterval);
return ans;
}
for( int i = 0; i < intervals.size(); i++ )
{
int chStart = intervals.get(i).start;
int chEnd = intervals.get(i).end;
//Check for overlap condition
if( Math.max(chStart,mStart) > Math.min(chEnd, mEnd))
{
ans.add(intervals.get(i));
count++;
}
//Condition for overlap
else
{
inter.start = Math.min(mStart,chStart);
inter.end = Math.max(mEnd, chEnd);
mStart = inter.start;
mEnd = inter.end;
if(!ans.contains(inter))
{
ans.add(inter);
}
}
}
//Condition when interval is larger than all elements in array, insert interval
//in final answer
if( count == intervals.size())
ans.add(newInterval);
//Sorting the arraylist according to start time using an inner class
//Helps in modularity
Collections.sort(ans, new IntervalSort());
//Time complexity: O(n)
//Space complexity: O(n)
/* for( int i = 0; i < intervals.size(); i++ )
{
int chStart = intervals.get(i).start;
int chEnd = intervals.get(i).end;
if( (chStart <= mStart) && (chEnd > mStart) )
{
inter.start = chStart;
for( int j = i + 1; j < intervals.size(); j++)
{
chStart = intervals.get(j).start;
chEnd = intervals.get(j).end;
if( (chStart <= mEnd) && (mEnd < chEnd) )
inter.end = chEnd;
ans.add(intervals.get(j));
}
}
ans.add(intervals.get(i));
}*/
return ans;
}
class IntervalSort implements Comparator<Interval>
{
public int compare(Interval arr1, Interval arr2)
{
return arr1.start - arr2.start;
}
}
}
The commented portion is an approach that I had tried before I settled for this newer one
I have these questions regarding my code:
1) How can I further optimize my code in time and space complexity?
2) Are there any grave java coding conventions that I have violated?
3) Is my code too redundant or am I using too many unnecessary variables?
4) How can I make sure that I don't miss out on any of the test cases?
Interval
class, but the classSolution
depends on it. Is this a mistake? \$\endgroup\$