Skip to content

Commit 86b46a7

Browse files
committed
Add problem 3200: Maximum Height of a Triangle
1 parent bfa2125 commit 86b46a7

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,7 @@ pub mod problem_3192_minimum_operations_to_make_binary_array_elements_equal_to_o
21332133
pub mod problem_3194_minimum_average_of_smallest_and_largest_elements;
21342134
pub mod problem_3195_find_the_minimum_area_to_cover_all_ones_i;
21352135
pub mod problem_3196_maximize_total_cost_of_alternating_subarrays;
2136+
pub mod problem_3200_maximum_height_of_a_triangle;
21362137
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21372138

21382139
#[cfg(test)]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
fn max_even_rows(x: u32) -> u32 {
7+
x.isqrt()
8+
}
9+
10+
fn max_odd_rows(x: u32) -> u32 {
11+
((x * 4 + 1).isqrt() - 1) / 2
12+
}
13+
14+
fn get_max_rows(even_rows: u32, odd_rows: u32) -> u32 {
15+
if odd_rows < even_rows {
16+
odd_rows * 2 + 1
17+
} else {
18+
even_rows * 2
19+
}
20+
}
21+
22+
pub fn max_height_of_triangle(red: i32, blue: i32) -> i32 {
23+
let red = red.cast_unsigned();
24+
let blue = blue.cast_unsigned();
25+
let red_even_rows = Self::max_even_rows(red);
26+
let red_odd_rows = Self::max_odd_rows(red);
27+
let blue_even_rows = Self::max_even_rows(blue);
28+
let blue_odd_rows = Self::max_odd_rows(blue);
29+
30+
Self::get_max_rows(red_even_rows, blue_odd_rows)
31+
.max(Self::get_max_rows(blue_even_rows, red_odd_rows))
32+
.cast_signed()
33+
}
34+
}
35+
36+
// ------------------------------------------------------ snip ------------------------------------------------------ //
37+
38+
impl super::Solution for Solution {
39+
fn max_height_of_triangle(red: i32, blue: i32) -> i32 {
40+
Self::max_height_of_triangle(red, blue)
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
#[test]
47+
fn test_solution() {
48+
super::super::tests::run::<super::Solution>();
49+
}
50+
}
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 max_height_of_triangle(red: i32, blue: 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 = [((2, 4), 3), ((2, 1), 2), ((4, 9), 4)];
13+
14+
for ((red, blue), expected) in test_cases {
15+
assert_eq!(S::max_height_of_triangle(red, blue), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)