Skip to content

Commit 897b361

Browse files
authored
Merge pull request #1967 from PradnyaGaitonde/PradnyaGaitonde-patch-26
Create 0057-insert-interval.md
2 parents b902868 + 9c21005 commit 897b361

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
id: insert-interval
3+
title: Insert Interval(LeetCode)
4+
sidebar_label: 0057-Insert Interval
5+
tags:
6+
- Array
7+
description: You are given an array of non-overlapping intervals intervals. Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals. Return intervals after the insertion.
8+
---
9+
10+
## Problem Statement
11+
12+
You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [starti, endi]` represent the start and the end of the `ith` interval and `intervals` is sorted in ascending order by `starti`. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.
13+
14+
Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by `starti` and `intervals` still does not have any overlapping `intervals` (merge overlapping intervals if necessary).
15+
16+
Return `intervals` after the insertion.
17+
18+
Note that you don't need to modify `intervals` in-place. You can make a new array and return it.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
24+
```plaintext
25+
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
26+
Output: [[1,5],[6,9]]
27+
```
28+
29+
**Example 2:**
30+
31+
```plaintext
32+
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
33+
Output: [[1,2],[3,10],[12,16]]
34+
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
35+
```
36+
37+
### Constraints
38+
39+
- `0 <= intervals.length <= 104`
40+
- `intervals[i].length == 2`
41+
- `0 <= starti <= endi <= 105`
42+
- `intervals` is sorted by `starti` in ascending order.
43+
- `newInterval.length == 2`
44+
- `0 <= start <= end <= 105`
45+
46+
## Solution
47+
48+
### Approach
49+
50+
#### Algorithm
51+
52+
1. Initialize the Result List:
53+
* Create an empty list result to store the final merged intervals.
54+
2. Add Non-overlapping Intervals Before the New Interval:
55+
* Iterate through the list of intervals and add all intervals that end before the new interval starts to the result list.
56+
3. Merge Overlapping Intervals:
57+
* Continue iterating through the list and merge all intervals that overlap with the new interval. This is done by updating the new interval's start and end to be the minimum start and maximum end of the overlapping intervals.
58+
* Once all overlapping intervals are merged, add the new interval to the result list.
59+
4. Add Remaining Intervals:
60+
* Finally, add all the remaining intervals that start after the new interval ends to the result list.
61+
62+
#### Implementation
63+
64+
```Java
65+
import java.util.List;
66+
import java.util.LinkedList;
67+
68+
public class Interval {
69+
int start;
70+
int end;
71+
72+
Interval() {
73+
start = 0;
74+
end = 0;
75+
}
76+
77+
Interval(int s, int e) {
78+
start = s;
79+
end = e;
80+
}
81+
}
82+
83+
public class Solution {
84+
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
85+
List<Interval> result = new LinkedList<>();
86+
int i = 0;
87+
88+
// Add all the intervals ending before newInterval starts
89+
while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
90+
result.add(intervals.get(i++));
91+
}
92+
93+
// Merge all overlapping intervals to one considering newInterval
94+
while (i < intervals.size() && intervals.get(i).start <= newInterval.end) {
95+
newInterval = new Interval(
96+
Math.min(newInterval.start, intervals.get(i).start),
97+
Math.max(newInterval.end, intervals.get(i).end)
98+
);
99+
i++;
100+
}
101+
102+
// Add the union of intervals we got
103+
result.add(newInterval);
104+
105+
// Add all the rest
106+
while (i < intervals.size()) {
107+
result.add(intervals.get(i++));
108+
}
109+
110+
return result;
111+
}
112+
}
113+
```
114+
115+
### Complexity Analysis
116+
117+
- **Time complexity**: $O(N)$
118+
- **Space complexity**: $O(N)$
119+
120+
### Conclusion
121+
122+
The algorithm efficiently inserts a new interval into a sorted list of non-overlapping intervals by handling non-overlapping intervals before and after the new interval separately and merging overlapping intervals in the middle. The overall time complexity is O(n) and the space complexity is O(n), making it both time-efficient and space-efficient.

0 commit comments

Comments
 (0)