From fbf88bd6e6d362b218502226adce6ef716fac11b Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Fri, 1 Apr 2022 00:38:22 +0800 Subject: [PATCH] add: start of April 2022! --- cpp/2204/220401-CN.cpp | 34 ++++++++++++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 6 ++++++ cpp/CMakeLists.txt | 3 ++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220401-CN.cpp create mode 100644 cpp/2204/CMakeLists.txt diff --git a/cpp/2204/220401-CN.cpp b/cpp/2204/220401-CN.cpp new file mode 100644 index 0000000..f0219d2 --- /dev/null +++ b/cpp/2204/220401-CN.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +/** + * 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& arr) { + std::map s[2]; + for (int i : arr) + ++s[i >= 0][std::abs(i)]; + for (std::map& 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; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt new file mode 100644 index 0000000..1610c1f --- /dev/null +++ b/cpp/2204/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.21) +PROJECT(2204) + +SET(CMAKE_CXX_STANDARD 23) + +ADD_EXECUTABLE(2204 220401-CN.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index a674ca3..a2ee3f6 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -11,5 +11,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp) # ADD_SUBDIRECTORY(2112) # ADD_SUBDIRECTORY(2201) # ADD_SUBDIRECTORY(2202) -ADD_SUBDIRECTORY(2203) +# ADD_SUBDIRECTORY(2203) +ADD_SUBDIRECTORY(2204) ADD_SUBDIRECTORY(more)