2014.2.13 01:23
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]
.
Solution:
For the new interval, find out where the start and end value lies in at the old intervals. The search process can be linear or binary, as the intervals are sorted. But writing binary search for intervals won't be as easy as for conventional array.
When the positions of start and end are found, insert the new interval accordingly. I don't think words will be an easy way to explain things here. Maybe you can draw some example on your draft and figure it out.
Segment tree may be a bit overqualified for this job, but it's purely the insert operation. You insert a new interval and adjust the tree.
Total time complexity is O(n). Space complexity is O(1).
Accepted code:
1 // 1WA, 1AC, O(n) solution. 2 /** 3 * Definition for an interval. 4 * struct Interval { 5 * int start; 6 * int end; 7 * Interval() : start(0), end(0) {} 8 * Interval(int s, int e) : start(s), end(e) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { 14 int i, n; 15 int in1, in2; 16 int i1, i2; 17 int ll, rr; 18 vector<Interval> res; 19 20 n = (int)intervals.size(); 21 if (n == 0) { 22 res.push_back(newInterval); 23 return res; 24 } 25 26 ll = newInterval.start; 27 rr = newInterval.end; 28 29 if (ll < intervals[0].start) { 30 i1 = -1; 31 in1 = 0; 32 } else if (ll > intervals[n - 1].end) { 33 i1 = n - 1; 34 in1 = 0; 35 } else { 36 for (i = 0; i < n - 1; ++i) { 37 if (ll >= intervals[i].start && ll <= intervals[i].end) { 38 // inside the interval 39 i1 = i; 40 in1 = 1; 41 break; 42 } else if (ll > intervals[i].end && ll < intervals[i + 1].start) { 43 i1 = i; 44 in1 = 0; 45 break; 46 } 47 } 48 if (i == n - 1) { 49 i1 = i; 50 in1 = 1; 51 } 52 } 53 54 if (rr < intervals[0].start) { 55 i2 = -1; 56 in2 = 0; 57 } else if (rr > intervals[n - 1].end) { 58 i2 = n - 1; 59 in2 = 0; 60 } else { 61 for (i = 0; i < n - 1; ++i) { 62 if (rr >= intervals[i].start && rr <= intervals[i].end) { 63 // inside the interval 64 i2 = i; 65 in2 = 1; 66 break; 67 } else if (rr > intervals[i].end && rr < intervals[i + 1].start) { 68 i2 = i; 69 in2 = 0; 70 break; 71 } 72 } 73 if (i == n - 1) { 74 i2 = i; 75 in2 = 1; 76 } 77 } 78 79 for (i = 0; i < i1; ++i) { 80 res.push_back(intervals[i]); 81 } 82 83 if (i1 == -1) { 84 ll = newInterval.start; 85 } else if (in1 == 1) { 86 ll = intervals[i1].start; 87 } else { 88 res.push_back(intervals[i1]); 89 ll = newInterval.start; 90 } 91 if (i2 == -1) { 92 rr = newInterval.end; 93 } else if (in2 == 1) { 94 rr = intervals[i2].end; 95 } else { 96 rr = newInterval.end; 97 } 98 res.push_back(Interval(ll, rr)); 99 100 for (i = i2 + 1; i < n; ++i) { 101 res.push_back(intervals[i]); 102 } 103 104 return res; 105 } 106 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)