Skip to content

Commit f402289

Browse files
authored
Merge pull request #36 from jtr109/develop
Develop
2 parents 19e1b3c + a7d514e commit f402289

File tree

5 files changed

+82
-46
lines changed

5 files changed

+82
-46
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ members = [
3333
"evaluate_reverse_polish_notation",
3434
"partitioning_into_minimum_number_of_deci_binary_numbers",
3535
"maximum_product_of_word_lengths",
36+
"maximum_erasure_value",
3637
]

maximum_erasure_value/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "maximum_erasure_value"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

maximum_erasure_value/src/lib.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
pub struct Solution {}
2+
3+
impl Solution {
4+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
5+
let mut l = 0;
6+
let mut r = 0;
7+
let mut max = nums[l];
8+
loop {
9+
r += 1;
10+
if r == nums.len() {
11+
break;
12+
}
13+
if nums[l..r].contains(&nums[r]) {
14+
while nums[l] != nums[r] {
15+
l += 1;
16+
}
17+
l += 1;
18+
}
19+
let sum = nums[l..=r].iter().sum();
20+
if max < sum {
21+
max = sum;
22+
}
23+
}
24+
max
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn example_1() {
34+
let nums = vec![4, 2, 4, 5, 6];
35+
let expected = 17;
36+
assert_eq!(Solution::maximum_unique_subarray(nums), expected);
37+
}
38+
39+
#[test]
40+
fn example_2() {
41+
let nums = vec![5, 2, 1, 2, 5, 2, 1, 2, 5];
42+
let expected = 8;
43+
assert_eq!(Solution::maximum_unique_subarray(nums), expected);
44+
}
45+
46+
#[test]
47+
fn submission_test_1() {
48+
let nums = vec![1000];
49+
let expected = 1000;
50+
assert_eq!(Solution::maximum_unique_subarray(nums), expected);
51+
}
52+
}

maximum_product_of_word_lengths/src/lib.rs

+16-46
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,31 @@
11
pub struct Solution {}
22

33
impl Solution {
4-
fn share_common_letters(
5-
mask_cache: &mut Vec<i32>,
6-
i1: usize,
7-
w1: &String,
8-
i2: usize,
9-
w2: &String,
10-
) -> bool {
11-
let mask1 = match mask_cache.get(i1) {
12-
Some(&x) if x != 0 => x,
13-
_ => {
14-
let mask = w1
15-
.chars()
16-
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8));
17-
mask_cache[i1] = mask;
18-
mask
19-
}
20-
};
21-
let mask2 = match mask_cache.get(i2) {
22-
Some(&x) if x != 0 => x,
23-
_ => {
24-
let mask = w2
25-
.chars()
26-
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8));
27-
mask_cache[i2] = mask;
28-
mask
29-
}
30-
};
31-
mask1 & mask2 != 0
4+
fn cal_mask(word: &str) -> i32 {
5+
word.chars()
6+
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8))
327
}
338

34-
fn get_length(length_cache: &mut Vec<usize>, index: usize, word: &String) -> usize {
35-
match length_cache.get(index) {
36-
Some(&l) if l != 0 => l,
37-
_ => {
38-
let length = word.chars().count();
39-
length_cache[index] = length;
40-
length
41-
}
42-
}
9+
fn share_common_letters(mask_cache: &Vec<i32>, i1: usize, i2: usize) -> bool {
10+
let mask1 = mask_cache[i1];
11+
let mask2 = mask_cache[i2];
12+
mask1 & mask2 != 0
4313
}
4414

4515
pub fn max_product(words: Vec<String>) -> i32 {
46-
let mut length_cache = vec![0; words.len()];
47-
let mut mask_cache = vec![0; words.len()];
16+
let mut length_cache = Vec::with_capacity(words.len());
17+
let mut mask_cache = Vec::with_capacity(words.len());
18+
for word in words.iter() {
19+
length_cache.push(word.chars().count() as i32);
20+
mask_cache.push(Self::cal_mask(word));
21+
}
4822
let mut max = 0;
49-
for (i, w1) in words.iter().enumerate() {
23+
for i in 0..words.len() {
5024
for j in i + 1..words.len() {
51-
let w2 = &words[j];
52-
if Self::share_common_letters(&mut mask_cache, i, w1, j, w2) {
25+
if Self::share_common_letters(&mask_cache, i, j) {
5326
continue;
5427
}
55-
max = max.max(
56-
(Self::get_length(&mut length_cache, i, w1)
57-
* Self::get_length(&mut length_cache, j, w2)) as i32,
58-
);
28+
max = max.max(length_cache[i] * length_cache[j]);
5929
}
6030
}
6131
max

0 commit comments

Comments
 (0)