From b282d4c0010a33d9dd63547695281989aa3a7e1c Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 15 Oct 2024 14:14:54 +0800 Subject: [PATCH] 20241015 finished. --- src/problem/mod.rs | 3 +- .../p3200_maximum_height_of_a_triangle.rs | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/problem/p3200_maximum_height_of_a_triangle.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index eb9d9a2..0d0040e 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -263,4 +263,5 @@ mod p3162_find_the_number_of_good_pairs_i; mod p3164_find_the_number_of_good_pairs_ii; mod p3158_find_the_xor_of_numbers_which_appear_twice; mod p1884_egg_drop_with_2_eggs_and_n_floors; -mod p887_super_egg_drop; \ No newline at end of file +mod p887_super_egg_drop; +mod p3200_maximum_height_of_a_triangle; \ No newline at end of file diff --git a/src/problem/p3200_maximum_height_of_a_triangle.rs b/src/problem/p3200_maximum_height_of_a_triangle.rs new file mode 100644 index 0000000..9d6774b --- /dev/null +++ b/src/problem/p3200_maximum_height_of_a_triangle.rs @@ -0,0 +1,52 @@ +/** + * [3200] Maximum Height of a Triangle + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn max_height_of_triangle(red: i32, blue: i32) -> i32 { + let mut result = 0; + let mut row = 1; + let mut switch = true; + let (mut first_count, mut second_count) = (0, 0); + + loop { + if switch { + first_count += row; + } else { + second_count += row; + } + + if first_count > red || second_count > blue { + if second_count > red || first_count > blue { + break; + } + } + + switch = !switch; + row += 1; + result += 1; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3200() { + assert_eq!(3, Solution::max_height_of_triangle(2, 4)); + assert_eq!(2, Solution::max_height_of_triangle(2, 1)); + assert_eq!(1, Solution::max_height_of_triangle(1, 1)); + assert_eq!(2, Solution::max_height_of_triangle(10, 1)); + assert_eq!(5, Solution::max_height_of_triangle(10, 10)); + } +}