-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2139: Minimum Moves to Reach Target Score
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/problem_2139_minimum_moves_to_reach_target_score/greedy.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn min_moves(target: i32, max_doubles: i32) -> i32 { | ||
let mut target = target as u32; | ||
let mut max_doubles = max_doubles as u32; | ||
let mut result = 0; | ||
|
||
if target > 1 { | ||
loop { | ||
if max_doubles == 0 { | ||
result += target - 1; | ||
|
||
break; | ||
} | ||
|
||
result += (target & 1) + 1; | ||
target >>= 1; | ||
|
||
if target > 1 { | ||
max_doubles -= 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_moves(target: i32, max_doubles: i32) -> i32 { | ||
Self::min_moves(target, max_doubles) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_2139_minimum_moves_to_reach_target_score/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn min_moves(target: i32, max_doubles: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [((5, 0), 4), ((19, 2), 7), ((10, 4), 4), ((1, 100), 0)]; | ||
|
||
for ((target, max_doubles), expected) in test_cases { | ||
assert_eq!(S::min_moves(target, max_doubles), expected); | ||
} | ||
} | ||
} |