-
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 1718: Construct the Lexicographically Largest Valid Sequence
- Loading branch information
Showing
3 changed files
with
92 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
73 changes: 73 additions & 0 deletions
73
src/problem_1718_construct_the_lexicographically_largest_valid_sequence/iterative.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,73 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn helper(buffer: &mut [i32], available: u32) -> bool { | ||
buffer.split_first_mut().map_or(true, |(first, rest)| { | ||
if *first == 0 { | ||
let mut bits = available; | ||
|
||
while bits != 0 { | ||
let candidate = 32 - bits.trailing_zeros(); | ||
let bit = 1 << bits.trailing_zeros(); | ||
|
||
if candidate == 1 { | ||
*first = candidate as _; | ||
|
||
if Self::helper(rest, available ^ bit) { | ||
return true; | ||
} | ||
|
||
*first = 0; | ||
} else { | ||
let other_index = candidate as usize - 1; | ||
|
||
if rest.get(other_index).copied() == Some(0) { | ||
*first = candidate as _; | ||
rest[other_index] = candidate as _; | ||
|
||
if Self::helper(rest, available ^ bit) { | ||
return true; | ||
} | ||
|
||
*first = 0; | ||
rest[other_index] = 0; | ||
} | ||
} | ||
|
||
bits ^= bit; | ||
} | ||
|
||
false | ||
} else { | ||
Self::helper(rest, available) | ||
} | ||
}) | ||
} | ||
|
||
pub fn construct_distanced_sequence(n: i32) -> Vec<i32> { | ||
let n = n as u32; | ||
let mut result = vec![0; n as usize * 2 - 1]; | ||
|
||
Self::helper(&mut result, u32::MAX << (32 - n)); | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn construct_distanced_sequence(n: i32) -> Vec<i32> { | ||
Self::construct_distanced_sequence(n) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1718_construct_the_lexicographically_largest_valid_sequence/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 iterative; | ||
|
||
pub trait Solution { | ||
fn construct_distanced_sequence(n: i32) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(3, &[3, 1, 2, 3, 2] as &[_]), (5, &[5, 3, 1, 4, 3, 5, 2, 4, 2])]; | ||
|
||
for (n, expected) in test_cases { | ||
assert_eq!(S::construct_distanced_sequence(n), expected); | ||
} | ||
} | ||
} |