Skip to content

Commit f700868

Browse files
committed
Add problem 3184: Count Pairs That Form a Complete Day I
1 parent a95199e commit f700868

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,7 @@ pub mod problem_3174_clear_digits;
21242124
pub mod problem_3175_find_the_first_player_to_win_k_games_in_a_row;
21252125
pub mod problem_3178_find_the_child_who_has_the_ball_after_k_seconds;
21262126
pub mod problem_3179_find_the_n_th_value_after_k_seconds;
2127+
pub mod problem_3184_count_pairs_that_form_a_complete_day_i;
21272128
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21282129

21292130
#[cfg(test)]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod modular_arithmetic;
2+
3+
pub trait Solution {
4+
fn count_complete_day_pairs(hours: 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 = [(&[12, 12, 30, 24, 24] as &[_], 2), (&[72, 48, 24, 3], 3)];
13+
14+
for (hours, expected) in test_cases {
15+
assert_eq!(S::count_complete_day_pairs(hours.to_vec()), expected);
16+
}
17+
}
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn count_complete_day_pairs(hours: Vec<i32>) -> i32 {
7+
let mut counts = [0; 24];
8+
let mut result = 0;
9+
10+
for hour in hours {
11+
let hour = (hour.cast_unsigned() % 24) as usize;
12+
13+
result += counts[if hour == 0 { 0 } else { 24 - hour }];
14+
counts[hour] += 1;
15+
}
16+
17+
result
18+
}
19+
}
20+
21+
// ------------------------------------------------------ snip ------------------------------------------------------ //
22+
23+
impl super::Solution for Solution {
24+
fn count_complete_day_pairs(hours: Vec<i32>) -> i32 {
25+
Self::count_complete_day_pairs(hours)
26+
}
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
#[test]
32+
fn test_solution() {
33+
super::super::tests::run::<super::Solution>();
34+
}
35+
}

0 commit comments

Comments
 (0)