Skip to content

Commit

Permalink
Add problem 1764: Form Array by Concatenating Subarrays of Another Array
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 29, 2024
1 parent d2107af commit bed0e6b
Show file tree
Hide file tree
Showing 3 changed files with 116 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 @@ -1315,6 +1315,7 @@ pub mod problem_1758_minimum_changes_to_make_alternating_binary_string;
pub mod problem_1759_count_number_of_homogenous_substrings;
pub mod problem_1760_minimum_limit_of_balls_in_a_bag;
pub mod problem_1763_longest_nice_substring;
pub mod problem_1764_form_array_by_concatenating_subarrays_of_another_array;
pub mod problem_1765_map_of_highest_peak;
pub mod problem_1768_merge_strings_alternately;
pub mod problem_1769_minimum_number_of_operations_to_move_all_balls_to_each_box;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
fn build_kmp_table(values: &[i32], target: &mut [usize]) {
target[0] = 0;

let mut matched = 0;
let mut i = 1;

while let Some(&value) = values.get(i) {
loop {
if value == values[matched] {
matched += 1;

break;
} else if let Some(&next_matched) = target.get(matched.wrapping_sub(1)) {
matched = next_matched;
} else {
break;
}
}

target[i] = matched;
i += 1;
}
}

pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
let mut iter = nums.iter().copied();
let mut kmp_table = Vec::new();

for group in groups {
kmp_table.resize(group.len(), 0);

Self::build_kmp_table(&group, &mut kmp_table);

let mut matched = 0;

'outer: while let Some(&expected) = group.get(matched) {
let mut expected = expected;

loop {
if let Some(value) = iter.next() {
loop {
if value == expected {
matched += 1;

continue 'outer;
} else if let Some(&next_matched) = kmp_table.get(matched.wrapping_sub(1)) {
matched = next_matched;
expected = group[matched];
} else {
break;
}
}
} else {
return false;
}
}
}
}

true
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
Self::can_choose(groups, nums)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub mod kmp;

pub trait Solution {
fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(
(
&[&[1, -1, -1] as &[_], &[3, -2, 0]] as &[&[_]],
&[1, -1, 0, 1, -1, -1, 3, -2, 0] as &[_],
),
true,
),
((&[&[10, -2], &[1, 2, 3, 4]], &[1, 2, 3, 4, 10, -2]), false),
((&[&[1, 2, 3], &[3, 4]], &[7, 7, 1, 2, 3, 4, 7, 7]), false),
((&[&[21, 22, 21, 22, 21, 30]], &[21, 22, 21, 22, 21, 22, 21, 30]), true),
];

for ((groups, nums), expected) in test_cases {
assert_eq!(
S::can_choose(groups.iter().copied().map(<[_]>::to_vec).collect(), nums.to_vec()),
expected,
);
}
}
}

0 comments on commit bed0e6b

Please sign in to comment.