Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
maximum-product-of-word-lengths.cpp 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2015-12-16 07:48 +08:00 . Update maximum-product-of-word-lengths.cpp
// Time: O(n) ~ O(n^2)
// Space: O(n)
// Counting Sort + Pruning + Bit Manipulation
class Solution {
public:
int maxProduct(vector<string>& words) {
words = counting_sort(words);
vector<int> bits(words.size());
for (int i = 0; i < words.size(); ++i) {
for (const auto& c : words[i]) {
bits[i] |= (1 << (c - 'a'));
}
}
int max_product = 0;
for (int i = 0; i + 1 < words.size() && pow(words[i].length(), 2) > max_product; ++i) {
for (int j = i + 1; j < words.size() && words[i].length() * words[j].length() > max_product; ++j) {
if (!(bits[i] & bits[j])) {
max_product = words[i].length() * words[j].length();
}
}
}
return max_product;
}
vector<string> counting_sort(const vector<string>& words) {
const int k = 1000; // k is max length of words in the dictionary
vector<vector<string>> buckets(k);
for (const auto& word : words) {
buckets[word.length()].emplace_back(word);
}
vector<string> res;
for (int i = k - 1; i >= 0; --i) {
if (!buckets[i].empty()) {
move(buckets[i].begin(), buckets[i].end(), back_inserter(res));
}
}
return res;
}
};
// Time: O(nlogn) ~ O(n^2)
// Space: O(n)
// Sorting + Pruning + Bit Manipulation
class Solution2 {
public:
int maxProduct(vector<string>& words) {
sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.length() > b.length(); });
vector<int> bits(words.size());
for (int i = 0; i < words.size(); ++i) {
for (const auto& c : words[i]) {
bits[i] |= (1 << (c - 'a'));
}
}
int max_product = 0;
for (int i = 0; i + 1 < words.size() && pow(words[i].length(), 2) > max_product; ++i) {
for (int j = i + 1; j < words.size() && words[i].length() * words[j].length() > max_product; ++j) {
if (!(bits[i] & bits[j])) {
max_product = words[i].length() * words[j].length();
}
}
}
return max_product;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助