Skip to content

Develop #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ members = [
"evaluate_reverse_polish_notation",
"partitioning_into_minimum_number_of_deci_binary_numbers",
"maximum_product_of_word_lengths",
"maximum_erasure_value",
]
9 changes: 9 additions & 0 deletions maximum_erasure_value/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "maximum_erasure_value"
version = "0.1.0"
authors = ["Ryan Li <conbas2019@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
52 changes: 52 additions & 0 deletions maximum_erasure_value/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pub struct Solution {}

impl Solution {
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
let mut l = 0;
let mut r = 0;
let mut max = nums[l];
loop {
r += 1;
if r == nums.len() {
break;
}
if nums[l..r].contains(&nums[r]) {
while nums[l] != nums[r] {
l += 1;
}
l += 1;
}
let sum = nums[l..=r].iter().sum();
if max < sum {
max = sum;
}
}
max
}
}

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

#[test]
fn example_1() {
let nums = vec![4, 2, 4, 5, 6];
let expected = 17;
assert_eq!(Solution::maximum_unique_subarray(nums), expected);
}

#[test]
fn example_2() {
let nums = vec![5, 2, 1, 2, 5, 2, 1, 2, 5];
let expected = 8;
assert_eq!(Solution::maximum_unique_subarray(nums), expected);
}

#[test]
fn submission_test_1() {
let nums = vec![1000];
let expected = 1000;
assert_eq!(Solution::maximum_unique_subarray(nums), expected);
}
}
62 changes: 16 additions & 46 deletions maximum_product_of_word_lengths/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,31 @@
pub struct Solution {}

impl Solution {
fn share_common_letters(
mask_cache: &mut Vec<i32>,
i1: usize,
w1: &String,
i2: usize,
w2: &String,
) -> bool {
let mask1 = match mask_cache.get(i1) {
Some(&x) if x != 0 => x,
_ => {
let mask = w1
.chars()
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8));
mask_cache[i1] = mask;
mask
}
};
let mask2 = match mask_cache.get(i2) {
Some(&x) if x != 0 => x,
_ => {
let mask = w2
.chars()
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8));
mask_cache[i2] = mask;
mask
}
};
mask1 & mask2 != 0
fn cal_mask(word: &str) -> i32 {
word.chars()
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8))
}

fn get_length(length_cache: &mut Vec<usize>, index: usize, word: &String) -> usize {
match length_cache.get(index) {
Some(&l) if l != 0 => l,
_ => {
let length = word.chars().count();
length_cache[index] = length;
length
}
}
fn share_common_letters(mask_cache: &Vec<i32>, i1: usize, i2: usize) -> bool {
let mask1 = mask_cache[i1];
let mask2 = mask_cache[i2];
mask1 & mask2 != 0
}

pub fn max_product(words: Vec<String>) -> i32 {
let mut length_cache = vec![0; words.len()];
let mut mask_cache = vec![0; words.len()];
let mut length_cache = Vec::with_capacity(words.len());
let mut mask_cache = Vec::with_capacity(words.len());
for word in words.iter() {
length_cache.push(word.chars().count() as i32);
mask_cache.push(Self::cal_mask(word));
}
let mut max = 0;
for (i, w1) in words.iter().enumerate() {
for i in 0..words.len() {
for j in i + 1..words.len() {
let w2 = &words[j];
if Self::share_common_letters(&mut mask_cache, i, w1, j, w2) {
if Self::share_common_letters(&mask_cache, i, j) {
continue;
}
max = max.max(
(Self::get_length(&mut length_cache, i, w1)
* Self::get_length(&mut length_cache, j, w2)) as i32,
);
max = max.max(length_cache[i] * length_cache[j]);
}
}
max
Expand Down