add: start of April 2022!

This commit is contained in:
Lam Haoyin 2022-04-01 00:38:22 +08:00
parent 0ccea4d27b
commit fbf88bd6e6
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
3 changed files with 42 additions and 1 deletions

34
cpp/2204/220401-CN.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <vector>
#include <map>
#include <iostream>
/**
* 954. Array of Doubled Pairs
* Given an integer array of even length arr, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.
*/
class Solution {
public:
static bool canReorderDoubled(const std::vector<int>& arr) {
std::map<int, int> s[2];
for (int i : arr)
++s[i >= 0][std::abs(i)];
for (std::map<int, int>& m : s) {
while (!m.empty()) {
int n = m.begin()->first;
if (!--m[n])
m.erase(n);
if (!m.count(2 * n))
return false;
if (!--m[2 * n])
m.erase(2 * n);
}
}
return true;
}
};
int main() {
std::cout << Solution::canReorderDoubled({2,4,0,0,8,1});
return 0;
}

6
cpp/2204/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220401-CN.cpp)

View File

@ -11,5 +11,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp)
# ADD_SUBDIRECTORY(2112) # ADD_SUBDIRECTORY(2112)
# ADD_SUBDIRECTORY(2201) # ADD_SUBDIRECTORY(2201)
# ADD_SUBDIRECTORY(2202) # ADD_SUBDIRECTORY(2202)
ADD_SUBDIRECTORY(2203) # ADD_SUBDIRECTORY(2203)
ADD_SUBDIRECTORY(2204)
ADD_SUBDIRECTORY(more) ADD_SUBDIRECTORY(more)