Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions insert-interval/DaleSeo.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 풀이 좋네요. 많이 배웠습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TC: O(n)
// SC: O(n)
impl Solution {
pub fn insert(intervals: Vec<Vec<i32>>, mut new_interval: Vec<i32>) -> Vec<Vec<i32>> {
let mut result = Vec::with_capacity(intervals.len() + 1);
let mut iter = intervals.into_iter().peekable();

while let Some(interval) = iter.next_if(|i| i[1] < new_interval[0]) {
result.push(interval);
}
while let Some(interval) = iter.next_if(|i| i[0] <= new_interval[1]) {
new_interval[0] = new_interval[0].min(interval[0]);
new_interval[1] = new_interval[1].max(interval[1]);
}
result.push(new_interval);
result.extend(iter);

result
}
}
Loading