-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_interval_57.rs
executable file
·39 lines (38 loc) · 1.81 KB
/
insert_interval_57.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
pub struct Solution {}
impl Solution {
pub fn insert(intervals: Vec<Vec<i32>>, new_interval: Vec<i32>) -> Vec<Vec<i32>> {
let mut answer: Vec<Vec<i32>> = Vec::with_capacity(intervals.len() + 1);
let mut i = 0;
//here I am comparing intervals and trying to find instances of overlap
//I then store these instances of overlap temperarily in the answer vector.
while i < intervals.len() && intervals[i][1] < new_interval[0] {
answer.push(intervals[i].clone());
i += 1;
}
//shadowing the variable with a mutable version of itself (like a boss)
let mut new_interval = new_interval;
//: using the new_interval variable to store wherever a merge would need to happen
while i < intervals.len() && intervals[i][0] <= new_interval[1] {
//: the final merged start time will be the minimum of the current and the new
new_interval[0] = std::cmp::min(new_interval[0], intervals[i][0]);
//: the final merged end time will be the maximum of the current and the new
new_interval[1] = std::cmp::max(new_interval[1], intervals[i][1]);
i += 1;
}
//:pushing the merged intervals to the answer
answer.push(new_interval);
//: adding any unmerged intervals to the final answer
//reusing i because I know it is already going to be the index of the last merge+1,
//therefore I can just add everything found in the original array after that.
while i < intervals.len() {
answer.push(intervals[i].clone());
i += 1;
}
answer
}
}
fn main() {
let intervals: Vec<Vec<i32>> = vec![[1, 3].to_vec(), [6, 9].to_vec()];
let new_interval: Vec<i32> = vec![2, 5];
Solution::insert(intervals, new_interval);
}