-
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.
- Loading branch information
Showing
17 changed files
with
794 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
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,64 @@ | ||
use crate::data_structures::TreeNode; | ||
|
||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cell::RefCell; | ||
use std::collections::VecDeque; | ||
use std::mem; | ||
use std::rc::Rc; | ||
|
||
impl Solution { | ||
pub fn reverse_odd_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> { | ||
let mut queue = VecDeque::from([Rc::clone(root.as_ref().unwrap())]); | ||
|
||
'outer: loop { | ||
for _ in 0..queue.len() { | ||
let node = queue.pop_front().unwrap(); | ||
let node = node.borrow(); | ||
|
||
if let (Some(left), Some(right)) = (&node.left, &node.right) { | ||
queue.extend([Rc::clone(left), Rc::clone(right)]); | ||
} else { | ||
break 'outer; | ||
} | ||
} | ||
|
||
let mut iter = queue.iter(); | ||
|
||
while let (Some(left), Some(right)) = (iter.next(), iter.next_back()) { | ||
mem::swap(&mut left.borrow_mut().val, &mut right.borrow_mut().val); | ||
} | ||
|
||
for _ in 0..queue.len() { | ||
let node = queue.pop_front().unwrap(); | ||
let node = node.borrow(); | ||
|
||
if let (Some(left), Some(right)) = (&node.left, &node.right) { | ||
queue.extend([Rc::clone(left), Rc::clone(right)]); | ||
} else { | ||
break 'outer; | ||
} | ||
} | ||
} | ||
|
||
root | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn reverse_odd_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> { | ||
Self::reverse_odd_levels(root) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,33 @@ | ||
use crate::data_structures::TreeNode; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
pub mod bfs; | ||
|
||
pub trait Solution { | ||
fn reverse_odd_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[2, 3, 5, 8, 13, 21, 34] as &[_], &[2, 5, 3, 8, 13, 21, 34] as &[_]), | ||
(&[7, 13, 11], &[7, 11, 13]), | ||
( | ||
&[0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], | ||
&[0, 2, 1, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 1, 1], | ||
), | ||
]; | ||
|
||
for (root, expected) in test_cases { | ||
assert_eq!( | ||
S::reverse_odd_levels(test_utilities::make_tree(root.iter().copied().map(Some))), | ||
test_utilities::make_tree(expected.iter().copied().map(Some)), | ||
); | ||
} | ||
} | ||
} |
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,24 @@ | ||
pub mod trie; | ||
|
||
pub trait Solution { | ||
fn sum_prefix_scores(words: Vec<String>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&["abc", "ab", "bc", "b"] as &[_], &[5, 4, 3, 2] as &[_]), | ||
(&["abcd"], &[4]), | ||
]; | ||
|
||
for (words, expected) in test_cases { | ||
assert_eq!( | ||
S::sum_prefix_scores(words.iter().copied().map(str::to_string).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |
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,74 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
struct Node { | ||
count: u32, | ||
children: [u32; 26], | ||
} | ||
|
||
impl Solution { | ||
pub fn sum_prefix_scores(words: Vec<String>) -> Vec<i32> { | ||
let mut allocator = Vec::<Node>::with_capacity(words.len() * 2); | ||
let mut allocator_len = 0; | ||
let mut root = [u32::MAX; 26]; | ||
|
||
for word in &words { | ||
let mut node = &mut root; | ||
|
||
for c in word.bytes() { | ||
let c = usize::from(c - b'a'); | ||
let mut child_index = node[c] as usize; | ||
|
||
if child_index < allocator_len { | ||
allocator[child_index].count += 1; | ||
} else { | ||
node[c] = allocator_len as _; | ||
child_index = allocator_len; | ||
|
||
allocator.push(Node { | ||
count: 1, | ||
children: [u32::MAX; 26], | ||
}); | ||
|
||
allocator_len += 1; | ||
}; | ||
|
||
node = &mut allocator[child_index].children; | ||
} | ||
} | ||
|
||
words | ||
.into_iter() | ||
.map(|word| { | ||
let mut count = 0; | ||
let mut node = &root; | ||
|
||
for c in word.into_bytes() { | ||
let child = &allocator[node[usize::from(c) - usize::from(b'a')] as usize]; | ||
|
||
count += child.count; | ||
node = &child.children; | ||
} | ||
|
||
count as _ | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn sum_prefix_scores(words: Vec<String>) -> Vec<i32> { | ||
Self::sum_prefix_scores(words) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,27 @@ | ||
pub mod sort_by_key; | ||
|
||
pub trait Solution { | ||
fn sort_people(names: Vec<String>, heights: Vec<i32>) -> Vec<String>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
(&["Mary", "John", "Emma"] as &[_], &[180, 165, 170] as &[_]), | ||
&["Mary", "Emma", "John"] as &[_], | ||
), | ||
((&["Alice", "Bob", "Bob"], &[155, 185, 150]), &["Bob", "Alice", "Bob"]), | ||
]; | ||
|
||
for ((names, heights), expected) in test_cases { | ||
assert_eq!( | ||
S::sort_people(names.iter().copied().map(str::to_string).collect(), heights.to_vec()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |
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,42 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Reverse; | ||
use std::mem; | ||
|
||
impl Solution { | ||
pub fn sort_people(names: Vec<String>, heights: Vec<i32>) -> Vec<String> { | ||
let mut names = names; | ||
|
||
let mut sorting = names | ||
.iter_mut() | ||
.zip(heights) | ||
.map(|(name, height)| (height, mem::take(name))) | ||
.collect::<Vec<_>>(); | ||
|
||
sorting.sort_unstable_by_key(|&(height, _)| Reverse(height as u32)); | ||
|
||
for (target, (_, mut name)) in names.iter_mut().zip(sorting) { | ||
mem::swap(target, &mut name); | ||
} | ||
|
||
names | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn sort_people(names: Vec<String>, heights: Vec<i32>) -> Vec<String> { | ||
Self::sort_people(names, heights) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/problem_2419_longest_subarray_with_maximum_bitwise_and/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,71 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Ordering; | ||
|
||
impl Solution { | ||
pub fn longest_subarray(nums: Vec<i32>) -> i32 { | ||
let mut max_value = 0; | ||
let mut max_value_length = 0; | ||
let mut length = 0; | ||
let mut iter = nums.iter().map(|&x| x as u32); | ||
|
||
'outer: loop { | ||
if let Some(num) = iter.next() { | ||
match num.cmp(&max_value) { | ||
Ordering::Less => { | ||
max_value_length = max_value_length.max(length); | ||
|
||
loop { | ||
if let Some(num) = iter.next() { | ||
match num.cmp(&max_value) { | ||
Ordering::Less => continue, | ||
Ordering::Equal => {} | ||
Ordering::Greater => { | ||
max_value = num; | ||
max_value_length = 1; | ||
} | ||
} | ||
|
||
length = 1; | ||
|
||
break; | ||
} | ||
|
||
break 'outer; | ||
} | ||
} | ||
Ordering::Equal => length += 1, | ||
Ordering::Greater => { | ||
max_value = num; | ||
max_value_length = 1; | ||
length = 1; | ||
} | ||
} | ||
} else { | ||
max_value_length = max_value_length.max(length); | ||
|
||
break; | ||
} | ||
} | ||
|
||
max_value_length | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn longest_subarray(nums: Vec<i32>) -> i32 { | ||
Self::longest_subarray(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
Oops, something went wrong.