From 489d931f11b8c06f0c7856a839a0a713c8507140 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 4 Jan 2024 17:52:29 +0100 Subject: [PATCH] =?UTF-8?q?cpp:=20add=20=E2=80=9C2870.=20Minimum=20Number?= =?UTF-8?q?=20of=20Operations=20to=20Make=20Array=20Empty=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...mber-of-operations-to-make-array-empty.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 cpp/minimum-number-of-operations-to-make-array-empty.cpp diff --git a/cpp/minimum-number-of-operations-to-make-array-empty.cpp b/cpp/minimum-number-of-operations-to-make-array-empty.cpp new file mode 100644 index 0000000..7c97adf --- /dev/null +++ b/cpp/minimum-number-of-operations-to-make-array-empty.cpp @@ -0,0 +1,32 @@ +#include +#include + +class Solution { + public: + int minOperations(const std::vector &nums) { + std::map freqs; + for (const auto &x : nums) { + ++freqs[x]; + } + + int operations = 0; + + for (const auto &[_, count] : freqs) { + if (count == 1) { + return -1; + } + + switch (count % 3) { + case 0: + operations += count / 3; + break; + case 1: + case 2: + operations += count / 3 + 1; + break; + } + } + + return operations; + } +};