-
Notifications
You must be signed in to change notification settings - Fork 215
/
insert_interval.java
107 lines (88 loc) · 4.38 KB
/
insert_interval.java
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
/*
Insert Interval
In this implementation, the `Interval` struct represents an interval with a start and end value.
The `insert` function takes a sorted list of intervals and a new interval as input and returns a new
list of intervals after merging the new interval with the existing intervals.
Here's how the `insert` function works:
1. It initializes an empty `result` slice to store the merged intervals and sets the index `i` to 0.
2. It iterates over the existing intervals and adds intervals that end before the new interval starts to the
`result` slice.
3. It merges intervals that overlap with the new interval by updating the start and end values of the new
interval accordingly.
4. It adds the merged new interval to the `result` slice.
5. It adds any remaining intervals from the original list to the `result` slice.
6. Finally, it returns the `result` slice containing the merged intervals.
The `min` and `max` functions are helper functions to find the minimum and maximum of two integers.
In the `main` function, an example input is provided with a list of intervals and a new interval.
The `insert` function is called with these inputs, and the result is printed to the console.
Time Complexity:
The time complexity is O(n), where n is the number of intervals in the input list. This is because we need to
iterate through each interval in the list to merge and insert the new interval. In the worst case, we may
need to traverse all intervals in the list.
Space Complexity:
The space complexity is O(n), where n is the number of intervals in the input list. This is because we
create a new result slice to store the merged intervals, which can potentially contain all the intervals
from the input list plus the merged new interval. Therefore, the space required is proportional to the
number of intervals in the input list.
Overall, the algorithm has a linear time complexity and linear space complexity with respect to the number
of intervals in the input list.
*/
import java.util.ArrayList;
import java.util.List;
class Interval {
int start;
int end;
Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
public class InsertInterval {
public static List<Interval> insertInterval(List<Interval> intervals, Interval newInterval) {
List<Interval> result = new ArrayList<>();
// Traverse through each interval in the input list
// and perform the necessary merging and inserting.
for (Interval interval : intervals) {
// If the current interval ends before the new interval starts,
// add it to the result as it does not overlap.
if (interval.end < newInterval.start) {
result.add(interval);
}
// If the current interval starts after the new interval ends,
// add the new interval and update it to the current interval
// as there won't be any more overlap with subsequent intervals.
else if (interval.start > newInterval.end) {
result.add(newInterval);
newInterval = interval;
}
// If there is an overlap between the current interval and the new interval,
// merge them by updating the new interval's start and end.
else {
newInterval.start = Math.min(interval.start, newInterval.start);
newInterval.end = Math.max(interval.end, newInterval.end);
}
}
// Add the final merged or inserted interval to the result.
result.add(newInterval);
return result;
}
// Utility function to print the intervals.
public static void printIntervals(List<Interval> intervals) {
for (Interval interval : intervals) {
System.out.print("[" + interval.start + ", " + interval.end + "] ");
}
System.out.println();
}
public static void main(String[] args) {
// Example usage
List<Interval> intervals = new ArrayList<>();
intervals.add(new Interval(1, 3));
intervals.add(new Interval(6, 9));
Interval newInterval = new Interval(2, 5);
System.out.print("Original intervals: ");
printIntervals(intervals);
List<Interval> mergedIntervals = insertInterval(intervals, newInterval);
System.out.print("Merged intervals: ");
printIntervals(mergedIntervals);
}
}