From 2ea19afe3568585d9a4c67e7849f61978d7822e1 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sun, 29 May 2022 12:15:23 +0800 Subject: [PATCH] add: 220529 [cpp] --- cpp/2205/220529.cpp | 39 +++++++++++++++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220529.cpp diff --git a/cpp/2205/220529.cpp b/cpp/2205/220529.cpp new file mode 100644 index 0000000..93b5424 --- /dev/null +++ b/cpp/2205/220529.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +/** + * 318. Maximum Product of Word Lengths + * Given a string array words, return the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. If no such two words exist, return 0. + */ + +class Solution { +public: + static int maxProduct(const std::vector& words) { + const int n = words.size(); + std::vector> w; + w.reserve(n); + for (const std::string& s : words) { + uint32_t set = 0; + for (const char c : s) + set |= 1 << (c - 'a'); + w.emplace_back(s.length(), set); + } + int ret = 0; + for (int i = 0; i < n; ++i) { + const auto& [l1, s1] = w[i]; + for (int j = i + 1; j < n; ++j) { + const auto& [l2, s2] = w[j]; + if (!(s1 & s2)) + ret = std::max(ret, l1 * l2); + } + } + return ret; + } +}; + +int main() { + std::cout << Solution::maxProduct({"abcw","baz","foo","bar","xtfn","abcdef"}); + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index d1b0f60..3d7a7a7 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220528-CN.cpp) +ADD_EXECUTABLE(2205 220529.cpp)