-
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 2375: Construct Smallest Number From DI String
- Loading branch information
Showing
4 changed files
with
148 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
59 changes: 59 additions & 0 deletions
59
src/problem_2375_construct_smallest_number_from_di_string/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,59 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn smallest_number(pattern: String) -> String { | ||
let n = pattern.len(); | ||
let mut result = String::with_capacity(n + 1); | ||
let mut prev = b'I'; | ||
let mut length = 0; | ||
let mut c = b'1'; | ||
|
||
for direction in pattern.bytes() { | ||
if direction == prev { | ||
length += 1; | ||
} else { | ||
if prev == b'I' { | ||
result.extend((c - length..c).map(char::from)); | ||
length = 1; | ||
} else { | ||
result.extend((c - length..=c).map(char::from).rev()); | ||
length = 0; | ||
} | ||
|
||
prev = direction; | ||
} | ||
|
||
c += 1; | ||
} | ||
|
||
drop(pattern); | ||
|
||
let iter = (c - length..=c).map(char::from); | ||
|
||
if prev == b'I' { | ||
result.extend(iter); | ||
} else { | ||
result.extend(iter.rev()); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn smallest_number(pattern: String) -> String { | ||
Self::smallest_number(pattern) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/problem_2375_construct_smallest_number_from_di_string/greedy_2.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,69 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn smallest_number(pattern: String) -> String { | ||
let n = pattern.len(); | ||
let mut result = String::with_capacity(n + 1); | ||
let mut length = 0; | ||
let mut c = b'1'; | ||
let mut iter = pattern.bytes(); | ||
|
||
'outer: loop { | ||
if let Some(direction) = iter.next() { | ||
if direction == b'I' { | ||
length += 1; | ||
c += 1; | ||
} else { | ||
result.extend((c - length..c).map(char::from)); | ||
|
||
length = 2; | ||
c += 2; | ||
|
||
loop { | ||
if let Some(direction) = iter.next() { | ||
if direction == b'I' { | ||
break; | ||
} | ||
|
||
length += 1; | ||
c += 1; | ||
} else { | ||
result.extend((c - length..c).map(char::from).rev()); | ||
|
||
break 'outer; | ||
} | ||
} | ||
|
||
result.extend((c - length..c).map(char::from).rev()); | ||
length = 0; | ||
} | ||
} else { | ||
result.extend((c - length..=c).map(char::from)); | ||
|
||
break; | ||
} | ||
} | ||
|
||
drop(pattern); | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn smallest_number(pattern: String) -> String { | ||
Self::smallest_number(pattern) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/problem_2375_construct_smallest_number_from_di_string/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,19 @@ | ||
pub mod greedy; | ||
pub mod greedy_2; | ||
|
||
pub trait Solution { | ||
fn smallest_number(pattern: String) -> String; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [("IIIDIDDD", "123549876"), ("DDD", "4321"), ("DDDIII", "4321567")]; | ||
|
||
for (pattern, expected) in test_cases { | ||
assert_eq!(S::smallest_number(pattern.to_string()), expected); | ||
} | ||
} | ||
} |