[LeetCode] 57. Insert Interval
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
Given intervals
[1,3],[6,9]
, insert and merge [2,5]
in as [1,5],[6,9]
.
Example 2:
Given
Given
[1,2],[3,5],[6,7],[8,10],[12,16]
, insert and merge [4,9]
in as [1,2],[3,10],[12,16]
.
This is because the new interval
[4,9]
overlaps with [3,5],[6,7],[8,10]
.
Thought process:
This is a follow-up to problem 56. A naive solution is to add the new interval to the list. And the rest is the same as 56.
Solution 1:
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 | /** * 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 List<Interval> insert(List<Interval> intervals, Interval newInterval) { intervals.add(newInterval); if (intervals.size() == 0) { return intervals; } intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start)); List<Interval> merged = new ArrayList<>(); int start = intervals.get(0).start; int end = intervals.get(0).end; for (Interval interval : intervals) { if (end >= interval.start) { end = Math.max(end, interval.end); } else { merged.add(new Interval(start, end)); start = interval.start; end = interval.end; } } merged.add(new Interval(start, end)); return merged; } } |
This solution almost exceeds the time limit.
Time complexity:
Say n = number of intervals. Sorting takes O(nlogn). Merging takes O(n). The overall time complexity is O(nlogn).
The sorting is actually unnecessary because I can just iterate through the intervals and insert the new interval into the right spot.
Can we do better?
Yes! The naive solution is not using the fact that the intervals are non-overlapping as well as the intervals are already sorted.
Since the intervals are non-overlapping, we don't need to merge the original intervals. And because the intervals are sorted, we can iterate through the intervals, and compare an interval with the new interval. If they don't overlap, we can add the interval directly to the result list. Else, we merge the overlapped intervals with the new interval.
Solution 2:
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 | /** * 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; } * } */ class Solution { public List<Interval> insert(List<Interval> intervals, Interval newInterval) { List<Interval> inserted = new ArrayList<>(); if (intervals.size() == 0) { inserted.add(newInterval); return inserted; } int i = 0; while (i < intervals.size() && intervals.get(i).end < newInterval.start) { inserted.add(intervals.get(i)); i++; } int start = newInterval.start; int end = newInterval.end; while (i < intervals.size() && intervals.get(i).start <= end) { start = Math.min(start, intervals.get(i).start); end = Math.max(end, intervals.get(i).end); i++; } inserted.add(new Interval(start, end)); while (i < intervals.size()) { inserted.add(intervals.get(i)); i++; } return inserted; } } |
Time complexity:
The algorithm goes through every interval once. That takes O(n).
Comments
Post a Comment