Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b2aa791

Browse files
add max_product (#18)
1 parent c969799 commit b2aa791

File tree

1 file changed

+88
-6
lines changed

1 file changed

+88
-6
lines changed
Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1-
pub struct Solution {}
2-
impl Solution {
1+
// Time: O(n) ~ O(n^2)
2+
// Space: O(n)
3+
// Counting Sort + Pruning + Bit Manipulation
4+
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/maximum-product-of-word-lengths.cpp#L1-L40
5+
pub struct Solution1 {}
6+
impl Solution1 {
7+
pub fn max_product(words: Vec<String>) -> i32 {
8+
0
9+
}
10+
}
11+
12+
// Time: O(nlogn) ~ O(n^2)
13+
// Space: O(n)
14+
// Sorting + Pruning + Bit Manipulation
15+
pub struct Solution2 {}
16+
impl Solution2 {
17+
pub fn max_product(words: Vec<String>) -> i32 {
18+
let mut sorted_words = words.clone();
19+
sorted_words.sort_by(|a, b| b.len().cmp(&a.len()));
20+
let mut bits: Vec<i32> = vec![0; words.len()];
21+
for i in 0..sorted_words.len() {
22+
for c in sorted_words[i].chars() {
23+
bits[i] |= 1 << (c as u32 - 'a' as u32);
24+
}
25+
}
26+
let mut max_product: i32 = 0;
27+
let mut i: usize = 0;
28+
while i + 1 < sorted_words.len() && sorted_words[i].len().pow(2) as i32 > max_product {
29+
let mut j: usize = i + 1;
30+
while j < sorted_words.len()
31+
&& (sorted_words[i].len() * sorted_words[j].len()) as i32 > max_product
32+
{
33+
if bits[i] & bits[j] == 0 {
34+
max_product = (sorted_words[i].len() * sorted_words[j].len()) as i32;
35+
}
36+
j += 1;
37+
}
38+
i += 1;
39+
}
40+
max_product
41+
}
42+
}
43+
44+
45+
// # Time: O(n) ~ O(n^2)
46+
// # Space: O(n)
47+
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py#L1-L36
48+
pub struct Solution3 {}
49+
impl Solution3 {
50+
pub fn max_product(words: Vec<String>) -> i32 {
51+
0
52+
}
53+
}
54+
55+
// # Time: O(nlogn) ~ O(n^2)
56+
// # Space: O(n)
57+
// # Sorting + Pruning + Bit Manipulation
58+
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py#L38-L62
59+
pub struct Solution4 {}
60+
impl Solution4 {
361
pub fn max_product(words: Vec<String>) -> i32 {
462
0
563
}
@@ -11,8 +69,32 @@ mod tests {
1169

1270
#[test]
1371
fn test_max_product() {
14-
// assert_max_product!(Solution::max_product(vec!["abcw","baz","foo","bar","xtfn","abcdef"]), 16);
15-
// assert_max_product!(Solution::max_product(vec!["a","ab","abc","d","cd","bcd","abcd"]), 4);
16-
// assert_max_product!(Solution::max_product(vec!["a","aa","aaa","aaaa"]), 0);
72+
assert_eq!(
73+
Solution2::max_product(
74+
vec!["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
75+
.iter()
76+
.map(|&x| String::from(x))
77+
.collect()
78+
),
79+
16
80+
);
81+
assert_eq!(
82+
Solution2::max_product(
83+
vec!["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
84+
.iter()
85+
.map(|&x| String::from(x))
86+
.collect()
87+
),
88+
4
89+
);
90+
assert_eq!(
91+
Solution2::max_product(
92+
vec!["a", "aa", "aaa", "aaaa"]
93+
.iter()
94+
.map(|&x| String::from(x))
95+
.collect()
96+
),
97+
0
98+
);
1799
}
18-
}
100+
}

0 commit comments

Comments
 (0)