Skip to content

Commit

Permalink
[STAGING]
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 31, 2025
1 parent 2d50a14 commit c862dea
Show file tree
Hide file tree
Showing 30 changed files with 1,156 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: Coverage
on: push
on:
push:
branches:
- master
jobs:
coverage:
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/progress.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: Progress
on: push
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: Rust
on: push
on:
push:
branches:
- master
jobs:
rustfmt:
runs-on: ${{ matrix.os }}
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,19 @@ pub mod problem_2411_smallest_subarrays_with_maximum_bitwise_or;
pub mod problem_2412_minimum_money_required_before_transactions;
pub mod problem_2413_smallest_even_multiple;
pub mod problem_2414_length_of_the_longest_alphabetical_continuous_substring;
pub mod problem_2415_reverse_odd_levels_of_binary_tree;
pub mod problem_2416_sum_of_prefix_scores_of_strings;
pub mod problem_2418_sort_the_people;
pub mod problem_2419_longest_subarray_with_maximum_bitwise_and;
pub mod problem_2420_find_all_good_indices;
pub mod problem_2421_number_of_good_paths;
pub mod problem_2423_remove_letter_to_equalize_frequency;
pub mod problem_2425_bitwise_xor_of_all_pairings;
pub mod problem_2427_number_of_common_factors;
pub mod problem_2428_maximum_sum_of_an_hourglass;
pub mod problem_2429_minimize_xor;
pub mod problem_2432_the_employee_that_worked_on_the_longest_task;
pub mod problem_2433_find_the_original_array_of_prefix_xor;

#[cfg(test)]
mod test_utilities;
64 changes: 64 additions & 0 deletions src/problem_2415_reverse_odd_levels_of_binary_tree/bfs.rs
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>();
}
}
33 changes: 33 additions & 0 deletions src/problem_2415_reverse_odd_levels_of_binary_tree/mod.rs
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)),
);
}
}
}
24 changes: 24 additions & 0 deletions src/problem_2416_sum_of_prefix_scores_of_strings/mod.rs
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,
);
}
}
}
74 changes: 74 additions & 0 deletions src/problem_2416_sum_of_prefix_scores_of_strings/trie.rs
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>();
}
}
27 changes: 27 additions & 0 deletions src/problem_2418_sort_the_people/mod.rs
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,
);
}
}
}
42 changes: 42 additions & 0 deletions src/problem_2418_sort_the_people/sort_by_key.rs
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>();
}
}
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>();
}
}
Loading

0 comments on commit c862dea

Please sign in to comment.