57. Insert Interval
problem description
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.
Example 1:
Example 2:
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
algorithm thought
和上一题一样,这里更简单的是,已经直接排好序了。所以,我们只需要找出中间可能有交叉的interval。然后合并。前后没有交叉的interval,就不变,直接插入。
code
algorithm analysis
这里不需要排序,时间复杂度O(n)
Last updated