代码拉取完成,页面将自动刷新
// 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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。