Last active
March 19, 2021 21:49
-
-
Save hderms/1888797174709cc3995105dd50f861cb to your computer and use it in GitHub Desktop.
Iterate through two vectors of pre-sorted disjoint intervals, returning a vector which includes any intervals which represent any overlap between ranges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cmp::{min, max}; | |
impl Solution { | |
pub fn interval_intersection(first_list: Vec<Vec<i32>>, second_list: Vec<Vec<i32>>) -> Vec<Vec<i32>> { | |
let mut i: usize = 0; | |
let mut j: usize = 0; | |
let mut vec: Vec<Vec<i32>> = Vec::with_capacity(max(first_list.len(), second_list.len())); | |
while (i < first_list.len() && j < second_list.len()) { | |
let left = &first_list[i]; | |
let right = &second_list[j]; | |
if !(left[0] > right[1] || right[0] > left[1] ) { | |
let inner = vec!(max(left[0], right[0]), min(left[1], right[1])); | |
vec.push(inner); | |
} | |
if left[1] < right[1] { | |
i += 1; | |
} else { | |
j += 1; | |
} | |
} | |
vec | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment