Insert Interval – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
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.
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 bystarti
in ascending order.newInterval.length == 2
0 <= start <= end <= 105
C++ Insert Interval LeetCode Solution
class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
vector<Interval> res;
int index = 0;
while(index < intervals.size() && intervals[index].end < newInterval.start){
res.push_back(intervals[index++]);
}
while(index < intervals.size() && intervals[index].start <= newInterval.end){
newInterval.start = min(newInterval.start, intervals[index].start);
newInterval.end = max(newInterval.end, intervals[index].end);
index++;
}
res.push_back(newInterval);
while(index < intervals.size()){
res.push_back(intervals[index++]);
}
return res;
}
};
Java Insert Interval LeetCode Solution
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
List<Interval> result = new ArrayList<Interval>();
for (Interval i : intervals) {
if (newInterval == null || i.end < newInterval.start)
result.add(i);
else if (i.start > newInterval.end) {
result.add(newInterval);
result.add(i);
newInterval = null;
} else {
newInterval.start = Math.min(newInterval.start, i.start);
newInterval.end = Math.max(newInterval.end, i.end);
}
}
if (newInterval != null)
result.add(newInterval);
return result;
}
Python 3 Insert Interval LeetCode Solution
def insert(self, intervals, newInterval):
s, e = newInterval.start, newInterval.end
left = [i for i in intervals if i.end < s]
right = [i for i in intervals if i.start > e]
if left + right != intervals:
s = min(s, intervals[len(left)].start)
e = max(e, intervals[~len(right)].end)
return left + [Interval(s, e)] + right
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below