-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_interval.rs
59 lines (55 loc) · 1.78 KB
/
insert_interval.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
///
/// Problem: Insert Interval
///
/// You are given an array of **non-overlapping** intervals, where `intervals[i] = [start_i, end_i]`
/// and a new interval `[start, end]`. Insert this new interval into `intervals`, ensuring the final output
/// is also **non-overlapping**.
///
/// **Example 1:**
/// ```plaintext
/// Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
/// Output: [[1,5],[6,9]]
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
/// Output: [[1,2],[3,10],[12,16]]
/// Explanation: The new interval [4,8] merges with [3,5], [6,7], and [8,10].
/// ```
///
/// **Constraints:**
/// - `0 <= intervals.length <= 10^4`
/// - `intervals[i].length == 2`
/// - `0 <= start_i <= end_i <= 10^4`
/// - `0 <= start <= end <= 10^4`
///
/// # Solution:
/// - **Time Complexity:** `O(n)`
/// - **Space Complexity:** `O(n)`
impl Solution {
pub fn insert(intervals: Vec<Vec<i32>>, new_interval: Vec<i32>) -> Vec<Vec<i32>> {
let mut result = Vec::new();
let (mut new_start, mut new_end) = (new_interval[0], new_interval[1]);
let mut inserted = false;
for interval in intervals.iter() {
let (start, end) = (interval[0], interval[1]);
if end < new_start {
result.push(interval.clone());
} else if start > new_end {
if !inserted {
result.push(vec![new_start, new_end]);
inserted = true;
}
result.push(interval.clone());
} else {
new_start = new_start.min(start);
new_end = new_end.max(end);
}
}
if !inserted {
result.push(vec![new_start, new_end]);
}
result
}
}