diff --git a/cpp/2302/230228-CN.cpp b/cpp/2302/230228-CN.cpp new file mode 100644 index 0000000..21c49db --- /dev/null +++ b/cpp/2302/230228-CN.cpp @@ -0,0 +1,30 @@ +#include +#include + +/** + * 2363. Merge Similar Items + * + * You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties: + * + * items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item. + * The value of each item in items is unique. + * Return a 2D integer array ret where ret[i] = [valuei, weighti], with weighti being the sum of weights of all items with value valuei. + * + * Note: ret should be returned in ascending order by value. + */ + +class Solution { +public: + static std::vector> mergeSimilarItems(const std::vector>& items1, const std::vector>& items2) { + std::map m; + for (const auto &item: items1) + m[item[0]] += item[1]; + for (const auto &item: items2) + m[item[0]] += item[1]; + std::vector> ret; + std::transform(m.begin(), m.end(), std::back_inserter(ret), [](const auto& x) { + return std::vector{x.first, x.second}; + }); + return ret; + } +}; diff --git a/cpp/2302/CMakeLists.txt b/cpp/2302/CMakeLists.txt index 68331da..da0a23c 100644 --- a/cpp/2302/CMakeLists.txt +++ b/cpp/2302/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2302) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2302 230221.cpp) +ADD_EXECUTABLE(2302 230228-CN.cpp)