diff --git a/src/lib.rs b/src/lib.rs index ac8e17b..5df5d62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -326,6 +326,7 @@ pub mod problem_0423_reconstruct_original_digits_from_english; pub mod problem_0434_number_of_segments_in_a_string; pub mod problem_0437_path_sum_iii; pub mod problem_0438_find_all_anagrams_in_a_string; +pub mod problem_0441_arranging_coins; pub mod problem_0442_find_all_duplicates_in_an_array; pub mod problem_0443_string_compression; pub mod problem_0448_find_all_numbers_disappeared_in_an_array; diff --git a/src/problem_0441_arranging_coins/binary_search.rs b/src/problem_0441_arranging_coins/binary_search.rs new file mode 100644 index 0000000..5fd8919 --- /dev/null +++ b/src/problem_0441_arranging_coins/binary_search.rs @@ -0,0 +1,35 @@ +pub struct Solution; + +impl Solution { + pub fn arrange_coins(n: i32) -> i32 { + if n <= 1 { + return n; + } + let n = i64::from(n); + let mut left = 1; + let mut right = n; + while left < right { + let mid = left + (right - left) / 2; + if mid * (mid + 1) <= 2 * n { + left = mid + 1; + } else { + right = mid; + } + } + left as i32 - 1 + } +} + +impl super::Solution for Solution { + fn arrange_coins(n: i32) -> i32 { + Self::arrange_coins(n) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0441_arranging_coins/iterative.rs b/src/problem_0441_arranging_coins/iterative.rs new file mode 100644 index 0000000..d47c25a --- /dev/null +++ b/src/problem_0441_arranging_coins/iterative.rs @@ -0,0 +1,31 @@ +pub struct Solution; + +impl Solution { + pub fn arrange_coins(n: i32) -> i32 { + let mut result = 0; + let mut n = n; + for x in 1.. { + n -= x; + if n >= 0 { + result += 1; + } else { + break; + } + } + result + } +} + +impl super::Solution for Solution { + fn arrange_coins(n: i32) -> i32 { + Self::arrange_coins(n) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0441_arranging_coins/math.rs b/src/problem_0441_arranging_coins/math.rs new file mode 100644 index 0000000..2e176e0 --- /dev/null +++ b/src/problem_0441_arranging_coins/math.rs @@ -0,0 +1,21 @@ +pub struct Solution; + +impl Solution { + pub fn arrange_coins(n: i32) -> i32 { + ((-1.0 + 8.0f64.mul_add(f64::from(n), 1.0).sqrt()) / 2.0) as i32 + } +} + +impl super::Solution for Solution { + fn arrange_coins(n: i32) -> i32 { + Self::arrange_coins(n) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0441_arranging_coins/mod.rs b/src/problem_0441_arranging_coins/mod.rs new file mode 100644 index 0000000..3248606 --- /dev/null +++ b/src/problem_0441_arranging_coins/mod.rs @@ -0,0 +1,45 @@ +pub mod binary_search; +pub mod iterative; +pub mod math; + +pub trait Solution { + fn arrange_coins(n: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (0, 0), + (1, 1), + (2, 1), + (3, 2), + (4, 2), + (5, 2), + (6, 3), + (7, 3), + (8, 3), + (9, 3), + (10, 4), + (11, 4), + (12, 4), + (13, 4), + (14, 4), + (15, 5), + (8254, 127), + (0x0200_0FFE, 8191), + (0x0800_0000, 16383), + (0x1FFF_FFFD, 32767), + (0x6B8B_4567, 60070), + (0x79AB_CDEF, 63894), + (0x7FED_CBA9, 65517), + (0x7FFF_FFFF, 65535), + ]; + + for (n, expected) in test_cases { + assert_eq!(S::arrange_coins(n), expected); + } + } +}