add: 230513-CN

This commit is contained in:
Eatswap 2023-05-13 13:43:40 +08:00
parent 0ec5b02f1f
commit 3a01c0e2a2
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
1 changed files with 24 additions and 0 deletions

24
cpp/2305/LC230513CN.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <algorithm>
#include <vector>
#include <unordered_set>
/**
* 2441. Largest Positive Integer That Exists With Its Negative
*
* Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.
* Return the positive integer k. If there is no such integer, return -1.
*/
class LC230513 {
public:
static int findMaxK(const std::vector<int>&) noexcept;
};
int LC230513::findMaxK(const std::vector<int>& n) noexcept {
std::unordered_set<int> s(n.begin(), n.end());
int ret = -1;
for (int i: s)
if (s.count(-i))
ret = std::max(ret, std::abs(i));
return ret;
}