Skip to content

Commit 2bf8bd2

Browse files
committed
Add problem 3021: Alice and Bob Playing Flower Game
1 parent c14ee7c commit 2bf8bd2

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,7 @@ pub mod problem_3012_minimize_length_of_array_using_operations;
20502050
pub mod problem_3014_minimum_number_of_pushes_to_type_word_i;
20512051
pub mod problem_3016_minimum_number_of_pushes_to_type_word_ii;
20522052
pub mod problem_3019_number_of_changing_keys;
2053+
pub mod problem_3021_alice_and_bob_playing_flower_game;
20532054

20542055
#[cfg(test)]
20552056
mod test_utilities;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn flower_game(n: i32, m: i32) -> i64 {
7+
let n = u64::from(n.cast_unsigned());
8+
let m = u64::from(m.cast_unsigned());
9+
let even_1 = n / 2;
10+
let odd_1 = n - even_1;
11+
let even_2 = m / 2;
12+
let odd_2 = m - even_2;
13+
14+
(odd_1 * even_2 + even_1 * odd_2).cast_signed()
15+
}
16+
}
17+
18+
// ------------------------------------------------------ snip ------------------------------------------------------ //
19+
20+
impl super::Solution for Solution {
21+
fn flower_game(n: i32, m: i32) -> i64 {
22+
Self::flower_game(n, m)
23+
}
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
#[test]
29+
fn test_solution() {
30+
super::super::tests::run::<super::Solution>();
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod mathematical;
2+
3+
pub trait Solution {
4+
fn flower_game(n: i32, m: i32) -> i64;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [((3, 2), 3), ((1, 1), 0)];
13+
14+
for ((n, m), expected) in test_cases {
15+
assert_eq!(S::flower_game(n, m), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)