Skip to content

Commit

Permalink
Add problem 0441: Arranging Coins
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed May 22, 2024
1 parent b17aa0c commit bb53136
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions src/problem_0441_arranging_coins/binary_search.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
31 changes: 31 additions & 0 deletions src/problem_0441_arranging_coins/iterative.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
21 changes: 21 additions & 0 deletions src/problem_0441_arranging_coins/math.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
45 changes: 45 additions & 0 deletions src/problem_0441_arranging_coins/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}

0 comments on commit bb53136

Please sign in to comment.