57. Insert Interval LeetCode Solution
In this guide, you will get 57. Insert Interval LeetCode Solution with the best time and space complexity. The solution to Insert Interval problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Insert Interval solution in C++
- Insert Interval solution in Java
- Insert Interval solution in Python
- Additional Resources
Problem Statement of Insert Interval
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.
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).
Return intervals after the insertion.
Note that you don’t need to modify intervals in-place. You can make a new array and return it.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 105
intervals is sorted by starti in ascending order.
newInterval.length == 2
0 <= start <= end <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
57. Insert Interval LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals,
vector<int>& newInterval) {
const int n = intervals.size();
vector<vector<int>> ans;
int i = 0;
while (i < n && intervals[i][1] < newInterval[0])
ans.push_back(intervals[i++]);
// Merge overlapping intervals.
while (i < n && intervals[i][0] <= newInterval[1]) {
newInterval[0] = min(newInterval[0], intervals[i][0]);
newInterval[1] = max(newInterval[1], intervals[i][1]);
++i;
}
ans.push_back(newInterval);
while (i < n)
ans.push_back(intervals[i++]);
return ans;
}
};
/* code provided by PROGIEZ */
57. Insert Interval LeetCode Solution in Java
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
final int n = intervals.length;
List<int[]> ans = new ArrayList<>();
int i = 0;
while (i < n && intervals[i][1] < newInterval[0])
ans.add(intervals[i++]);
// Merge overlapping intervals.
while (i < n && intervals[i][0] <= newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
++i;
}
ans.add(newInterval);
while (i < n)
ans.add(intervals[i++]);
return ans.toArray(int[][] ::new);
}
}
// code provided by PROGIEZ
57. Insert Interval LeetCode Solution in Python
class Solution:
def insert(self, intervals: list[list[int]],
newInterval: list[int]) -> list[list[int]]:
n = len(intervals)
ans = []
i = 0
while i < n and intervals[i][1] < newInterval[0]:
ans.append(intervals[i])
i += 1
# Merge overlapping intervals.
while i < n and intervals[i][0] <= newInterval[1]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
i += 1
ans.append(newInterval)
while i < n:
ans.append(intervals[i])
i += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.