Skip to content

Commit

Permalink
added new MergeIntervals.java solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed Jun 4, 2018
1 parent e81f4e5 commit a904bdf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 110 deletions.
38 changes: 16 additions & 22 deletions company/facebook/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class MergeIntervals {
class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() <= 1) {
return intervals;
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}

// Sort by ascending starting point using an anonymous Comparator
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.sort(allIntervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
}
});

List<Interval> result = new ArrayList<Interval>();

int start = intervals.get(0).start;
int end = intervals.get(0).end;

for(Interval interval : intervals) {
if(interval.start <= end) {
end = Math.max(end, interval.end);
for(Interval i: allIntervals) {
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
result.add(i);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
}
}

result.add(new Interval(start, end));

return result;
}
}
38 changes: 16 additions & 22 deletions company/google/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class MergeIntervals {
class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() <= 1) {
return intervals;
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}

// Sort by ascending starting point using an anonymous Comparator
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.sort(allIntervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
}
});

List<Interval> result = new ArrayList<Interval>();

int start = intervals.get(0).start;
int end = intervals.get(0).end;

for(Interval interval : intervals) {
if(interval.start <= end) {
end = Math.max(end, interval.end);
for(Interval i: allIntervals) {
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
result.add(i);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
}
}

result.add(new Interval(start, end));

return result;
}
}
38 changes: 16 additions & 22 deletions company/linkedin/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class MergeIntervals {
class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() <= 1) {
return intervals;
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}

// Sort by ascending starting point using an anonymous Comparator
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.sort(allIntervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
}
});

List<Interval> result = new ArrayList<Interval>();

int start = intervals.get(0).start;
int end = intervals.get(0).end;

for(Interval interval : intervals) {
if(interval.start <= end) {
end = Math.max(end, interval.end);
for(Interval i: allIntervals) {
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
result.add(i);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
}
}

result.add(new Interval(start, end));

return result;
}
}
38 changes: 16 additions & 22 deletions company/twitter/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class MergeIntervals {
class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() <= 1) {
return intervals;
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}

// Sort by ascending starting point using an anonymous Comparator
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.sort(allIntervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
}
});

List<Interval> result = new ArrayList<Interval>();

int start = intervals.get(0).start;
int end = intervals.get(0).end;

for(Interval interval : intervals) {
if(interval.start <= end) {
end = Math.max(end, interval.end);
for(Interval i: allIntervals) {
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
result.add(i);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
}
}

result.add(new Interval(start, end));

return result;
}
}
38 changes: 16 additions & 22 deletions leetcode/array/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class MergeIntervals {
class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() <= 1) {
return intervals;
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}

// Sort by ascending starting point using an anonymous Comparator
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.sort(allIntervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
}
});

List<Interval> result = new ArrayList<Interval>();

int start = intervals.get(0).start;
int end = intervals.get(0).end;

for(Interval interval : intervals) {
if(interval.start <= end) {
end = Math.max(end, interval.end);
for(Interval i: allIntervals) {
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
result.add(i);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
}
}

result.add(new Interval(start, end));

return result;
}
}

0 comments on commit a904bdf

Please sign in to comment.