Skip to content

Commit c0ab54f

Browse files
committed
Add problem 3169: Count Days Without Meetings
1 parent 49c13d7 commit c0ab54f

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,7 @@ pub mod problem_3162_find_the_number_of_good_pairs_i;
21182118
pub mod problem_3163_string_compression_iii;
21192119
pub mod problem_3164_find_the_number_of_good_pairs_ii;
21202120
pub mod problem_3168_minimum_number_of_chairs_in_a_waiting_room;
2121+
pub mod problem_3169_count_days_without_meetings;
21212122
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21222123

21232124
#[cfg(test)]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod sweep_line;
2+
3+
pub trait Solution {
4+
fn count_days(days: i32, meetings: Vec<Vec<i32>>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((10, &[[5, 7], [1, 3], [9, 10]] as &[_]), 2),
14+
((5, &[[2, 4], [1, 3]]), 1),
15+
((6, &[[1, 6]]), 0),
16+
((8, &[[3, 4], [4, 8], [2, 5], [3, 8]]), 1),
17+
];
18+
19+
for ((days, meetings), expected) in test_cases {
20+
assert_eq!(S::count_days(days, meetings.iter().map(Vec::from).collect()), expected,);
21+
}
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn count_days(days: i32, meetings: Vec<Vec<i32>>) -> i32 {
7+
let days = days.cast_unsigned();
8+
9+
let mut meetings = meetings
10+
.into_iter()
11+
.flat_map(|meeting| {
12+
let [start, end] = <[_; 2]>::map(meeting.try_into().ok().unwrap(), i32::cast_unsigned);
13+
14+
[start << 2, (end << 2) | 2]
15+
})
16+
.collect::<Box<_>>();
17+
18+
meetings.sort_unstable();
19+
20+
let mut result = 0;
21+
let mut start = 1;
22+
let mut count = 0;
23+
24+
for event in meetings {
25+
count = count + 1 - (event & 3);
26+
27+
if count == 0 {
28+
start = (event >> 2) + 1;
29+
} else if start != 0 {
30+
result += (event >> 2) - start;
31+
start = 0;
32+
}
33+
}
34+
35+
result += days + 1 - start;
36+
37+
result.cast_signed()
38+
}
39+
}
40+
41+
// ------------------------------------------------------ snip ------------------------------------------------------ //
42+
43+
impl super::Solution for Solution {
44+
fn count_days(days: i32, meetings: Vec<Vec<i32>>) -> i32 {
45+
Self::count_days(days, meetings)
46+
}
47+
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
#[test]
52+
fn test_solution() {
53+
super::super::tests::run::<super::Solution>();
54+
}
55+
}

0 commit comments

Comments
 (0)