add: 230413-CN

This commit is contained in:
Eatswap 2023-04-13 11:51:14 +08:00
parent 8be4d84d34
commit d77ba88558
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 28 additions and 1 deletions

27
cpp/2304/230413-CN.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <iterator>
#include <unordered_map>
#include <vector>
#include <algorithm>
/**
* 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>&);
};
int Solution::mostFrequentEven(const std::vector<int>& n) {
std::unordered_map<int, int> 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;
}

View File

@ -4,4 +4,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true) SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2304 230412-CN.cpp) ADD_EXECUTABLE(2304 230413-CN.cpp)