From a904bdf37f85141bcbd17ad28ad6341e243aaf0e Mon Sep 17 00:00:00 2001 From: Kevin Naughton Jr Date: Sun, 3 Jun 2018 21:24:53 -0400 Subject: [PATCH] added new MergeIntervals.java solution --- company/facebook/MergeIntervals.java | 38 ++++++++++++---------------- company/google/MergeIntervals.java | 38 ++++++++++++---------------- company/linkedin/MergeIntervals.java | 38 ++++++++++++---------------- company/twitter/MergeIntervals.java | 38 ++++++++++++---------------- leetcode/array/MergeIntervals.java | 38 ++++++++++++---------------- 5 files changed, 80 insertions(+), 110 deletions(-) diff --git a/company/facebook/MergeIntervals.java b/company/facebook/MergeIntervals.java index c0a04d23..0546fb80 100644 --- a/company/facebook/MergeIntervals.java +++ b/company/facebook/MergeIntervals.java @@ -13,37 +13,31 @@ * Interval(int s, int e) { start = s; end = e; } * } */ -public class MergeIntervals { +class MergeIntervals { public List merge(List intervals) { - if(intervals.size() <= 1) { - return intervals; + List result = new ArrayList(); + if(intervals == null || intervals.size() == 0) { + return result; } - // Sort by ascending starting point using an anonymous Comparator - Collections.sort(intervals, new Comparator() { - @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() { + public int compare(Interval a, Interval b) { + if(a.start == b.start) { + return a.end - b.end; + } + return a.start - b.start; + } }); - List result = new ArrayList(); - - 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; } } diff --git a/company/google/MergeIntervals.java b/company/google/MergeIntervals.java index c0a04d23..0546fb80 100644 --- a/company/google/MergeIntervals.java +++ b/company/google/MergeIntervals.java @@ -13,37 +13,31 @@ * Interval(int s, int e) { start = s; end = e; } * } */ -public class MergeIntervals { +class MergeIntervals { public List merge(List intervals) { - if(intervals.size() <= 1) { - return intervals; + List result = new ArrayList(); + if(intervals == null || intervals.size() == 0) { + return result; } - // Sort by ascending starting point using an anonymous Comparator - Collections.sort(intervals, new Comparator() { - @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() { + public int compare(Interval a, Interval b) { + if(a.start == b.start) { + return a.end - b.end; + } + return a.start - b.start; + } }); - List result = new ArrayList(); - - 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; } } diff --git a/company/linkedin/MergeIntervals.java b/company/linkedin/MergeIntervals.java index c0a04d23..0546fb80 100644 --- a/company/linkedin/MergeIntervals.java +++ b/company/linkedin/MergeIntervals.java @@ -13,37 +13,31 @@ * Interval(int s, int e) { start = s; end = e; } * } */ -public class MergeIntervals { +class MergeIntervals { public List merge(List intervals) { - if(intervals.size() <= 1) { - return intervals; + List result = new ArrayList(); + if(intervals == null || intervals.size() == 0) { + return result; } - // Sort by ascending starting point using an anonymous Comparator - Collections.sort(intervals, new Comparator() { - @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() { + public int compare(Interval a, Interval b) { + if(a.start == b.start) { + return a.end - b.end; + } + return a.start - b.start; + } }); - List result = new ArrayList(); - - 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; } } diff --git a/company/twitter/MergeIntervals.java b/company/twitter/MergeIntervals.java index c0a04d23..0546fb80 100644 --- a/company/twitter/MergeIntervals.java +++ b/company/twitter/MergeIntervals.java @@ -13,37 +13,31 @@ * Interval(int s, int e) { start = s; end = e; } * } */ -public class MergeIntervals { +class MergeIntervals { public List merge(List intervals) { - if(intervals.size() <= 1) { - return intervals; + List result = new ArrayList(); + if(intervals == null || intervals.size() == 0) { + return result; } - // Sort by ascending starting point using an anonymous Comparator - Collections.sort(intervals, new Comparator() { - @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() { + public int compare(Interval a, Interval b) { + if(a.start == b.start) { + return a.end - b.end; + } + return a.start - b.start; + } }); - List result = new ArrayList(); - - 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; } } diff --git a/leetcode/array/MergeIntervals.java b/leetcode/array/MergeIntervals.java index c0a04d23..0546fb80 100644 --- a/leetcode/array/MergeIntervals.java +++ b/leetcode/array/MergeIntervals.java @@ -13,37 +13,31 @@ * Interval(int s, int e) { start = s; end = e; } * } */ -public class MergeIntervals { +class MergeIntervals { public List merge(List intervals) { - if(intervals.size() <= 1) { - return intervals; + List result = new ArrayList(); + if(intervals == null || intervals.size() == 0) { + return result; } - // Sort by ascending starting point using an anonymous Comparator - Collections.sort(intervals, new Comparator() { - @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() { + public int compare(Interval a, Interval b) { + if(a.start == b.start) { + return a.end - b.end; + } + return a.start - b.start; + } }); - List result = new ArrayList(); - - 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; } }