From d77ba88558d43ef070c15eeab483ff40e3707674 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Thu, 13 Apr 2023 11:51:14 +0800 Subject: [PATCH] add: 230413-CN --- cpp/2304/230413-CN.cpp | 27 +++++++++++++++++++++++++++ cpp/2304/CMakeLists.txt | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 cpp/2304/230413-CN.cpp diff --git a/cpp/2304/230413-CN.cpp b/cpp/2304/230413-CN.cpp new file mode 100644 index 0000000..64a06ed --- /dev/null +++ b/cpp/2304/230413-CN.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +/** + * 2404. Most Frequent Even Element + * + * Given an integer array nums, return the most frequent even element. + * If there is a tie, return the smallest one. If there is no such element, return -1. + */ + +class Solution { + using LL = long long; +public: + static int mostFrequentEven(const std::vector&); +}; + +int Solution::mostFrequentEven(const std::vector& n) { + std::unordered_map m; + std::for_each(n.begin(), n.end(), [&](int x) { if (!(x & 1)) ++m[x]; }); + return m.empty() ? -1 : std::max_element(m.begin(), m.end(), [](auto&& x, auto&& y) { + auto&& [xk, xs] = x; + auto&& [yk, ys] = y; + return (LL(xs) << 32) - xk < (LL(ys) << 32) - yk; + })->first; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 4fb59f1..49567ad 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230412-CN.cpp) +ADD_EXECUTABLE(2304 230413-CN.cpp)