LeetCode --- 57. Insert Interval

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:

Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:

Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

这道题的要求是给定一组非重叠且开始时间有序的间隔,在其中插入一个新的间隔。

当然,这道题可以借助Merge Intervals里面的代码,先将新的间隔加入到数组中,然后合并即可。时间复杂度是O(nlogn)。

事实上,也可以不排序,直接插入时间间隔,插入的时间间隔的位置可以分成三部分:

  • 插入位置左侧
  • 插入位置(有重叠或无重叠)
  • 插入位置右侧

这三个部分分别处理,只有在插入位置处理可能存在的情况即可。

时间复杂度:O(n)

空间复杂度:O(n)

 1 class Solution
 2 {
 3 public:
 4     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval)
 5     {
 6         vector<Interval> vi;
 7
 8         int i = 0;
 9         while(i < intervals.size() && intervals[i].end < newInterval.start)
10             vi.push_back(intervals[i ++]);
11
12         vi.push_back(newInterval);
13         while(i < intervals.size() && vi[vi.size() - 1].end >= intervals[i].start)
14         {
15             vi[vi.size() - 1].start = min(intervals[i].start, vi[vi.size() - 1].start);
16             vi[vi.size() - 1].end = max(intervals[i].end, vi[vi.size() - 1].end);
17             ++ i;
18         }
19
20          while(i < intervals.size())
21             vi.push_back(intervals[i ++]);
22
23         return vi;
24     }
25 };

转载请说明出处:LeetCode --- 57. Insert Interval

时间: 2024-08-06 03:42:55

LeetCode --- 57. Insert Interval的相关文章

LeetCode 57. Insert Interval / Add Interval (get total covered length)

57. Insert Interval 由于intervals已经有序,不需要排序.本题是要返回一个interval数组,所以并不需要对原数组进行改动. 方法一: 由于intervals有序,我们可以二分找到应该插入的位置,然后merge intervals即可. 时间复杂度 O(n) 方法二: 我们只需要找出overlap的部分,merge即可.别的没有overlap的部分保持不变. class Solution { public: vector<vector<int>> ins

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval 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: Given intervals [1,3],[6,9], i

[LeetCode] 57. Insert Interval 解决思路

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:Given intervals [1,3],[6,9], insert and merge [

LeetCode 57. Insert Interval 插入区间 (C++/Java)

题目: 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: Input: intervals = [[1,3],[6,9]], newInter

leetcode 57 Insert Interval ----- java

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:Given intervals [1,3],[6,9], insert and merge [

[leetcode]57. Insert Interval插入区间

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: Input: intervals = [[1,3],[6,9]], newInterval

No.57 Insert Interval

No.57 Insert Interval 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:Given intervals [1,3],[6,

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval 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 t

【LeetCode】Insert Interval

Insert Interval 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:Given intervals [1,3],[6,9], in