Skip to content

Commit 19e1b3c

Browse files
authored
Merge pull request #35 from jtr109/develop
Develop
2 parents 57c879a + 32452c9 commit 19e1b3c

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ members = [
3131
"find_and_replace_pattern",
3232
"to_lower_case",
3333
"evaluate_reverse_polish_notation",
34+
"partitioning_into_minimum_number_of_deci_binary_numbers",
35+
"maximum_product_of_word_lengths",
3436
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "maximum_product_of_word_lengths"
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]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
pub struct Solution {}
2+
3+
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
32+
}
33+
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+
}
43+
}
44+
45+
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()];
48+
let mut max = 0;
49+
for (i, w1) in words.iter().enumerate() {
50+
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) {
53+
continue;
54+
}
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+
);
59+
}
60+
}
61+
max
62+
}
63+
}
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
fn it_works(words: Vec<&str>, expected: i32) {
70+
assert_eq!(
71+
Solution::max_product(words.iter().map(|x| x.to_string()).collect()),
72+
expected
73+
);
74+
}
75+
76+
#[test]
77+
fn example_1() {
78+
let words = vec!["abcw", "baz", "foo", "bar", "xtfn", "abcdef"];
79+
let expected = 16;
80+
it_works(words, expected);
81+
}
82+
83+
#[test]
84+
fn example_2() {
85+
let words = vec!["a", "ab", "abc", "d", "cd", "bcd", "abcd"];
86+
let expected = 4;
87+
it_works(words, expected);
88+
}
89+
90+
#[test]
91+
fn example_3() {
92+
let words = vec!["a", "aa", "aaa", "aaaa"];
93+
let expected = 0;
94+
it_works(words, expected);
95+
}
96+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "partitioning_into_minimum_number_of_deci_binary_numbers"
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]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pub struct Solution {}
2+
3+
impl Solution {
4+
pub fn min_partitions(n: String) -> i32 {
5+
n.chars().max().unwrap() as i32 - '0' as i32
6+
}
7+
}
8+
9+
#[cfg(test)]
10+
mod tests {
11+
use super::*;
12+
13+
fn it_works(n: &str, expected: i32) {
14+
assert_eq!(Solution::min_partitions(n.to_string()), expected);
15+
}
16+
17+
#[test]
18+
fn example_1() {
19+
it_works("32", 3);
20+
}
21+
22+
#[test]
23+
fn example_2() {
24+
it_works("82734", 8);
25+
}
26+
27+
#[test]
28+
fn example_3() {
29+
it_works("27346209830709182346", 9);
30+
}
31+
}

0 commit comments

Comments
 (0)