add: 230413-CN
This commit is contained in:
parent
8be4d84d34
commit
d77ba88558
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue