leetcode_57
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.
NOTE:
input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
Solutions
straight forward
Collect intervals don't overlap with the newInterval.
collect intervals whose end is smaller than inserted interval' start.
collect intervals whose start is larger than inserted interval's end.
Merge overlapping intervals by finding the minimum start and the maximum end among them.
Choose the smallest between inserted intervals's start and the first overlapped interval's start as the start.
The same for choosing the end as the end.
Finally build a new interval with the start and end.
Concatenate these three parts.
Last updated
Was this helpful?