From 2c5037a943f615a8098d1c94bf69445216e7cc79 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 4 May 2022 09:26:12 +0430 Subject: update --- 1679/main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1679/main.cpp (limited to '1679/main.cpp') diff --git a/1679/main.cpp b/1679/main.cpp new file mode 100644 index 0000000..0d33152 --- /dev/null +++ b/1679/main.cpp @@ -0,0 +1,50 @@ + +#include "header.hpp" +#include + +class Solution { +public: + static int maxOperations(std::vector &nums, int k) { + int result = 0; + std::unordered_map dict; + for (auto &iter : nums) { + if (dict.find(iter) != dict.end()) { + dict[iter]++; + } else { + dict.insert(std::make_pair(iter, 1)); + } + } + for (auto &iter : dict) { + if (k > iter.first) { + if (dict.find(k - iter.first) != dict.end()) { + if (k - iter.first == iter.first) { + result = result + int(iter.second / 2); + } else { + auto dec = std::min(dict[iter.first], dict[k - iter.first]); + result = result + dec; + dict[iter.first] -= dec; + dict[k - iter.first] -= dec; + } + } + } + } + return result; + } +}; + +int main(int argc, char **argv) { + std::vector num1 = {1, 2, 3, 4}; + std::vector num2 = {3, 1, 3, 4, 3}; + std::vector num3 = {2, 2, 2, 3, 1, 1, 4, 1}; + std::vector num4 = {3, 5, 1, 5}; + std::vector num5 = {2, 5, 4, 4, 1, 3, 4, 4, 1, 4, + 4, 1, 2, 1, 2, 2, 3, 2, 4, 2}; + + std::cout << Solution::maxOperations(num1, 5) << std::endl; // 2 + std::cout << Solution::maxOperations(num2, 6) << std::endl; // 1 + std::cout << Solution::maxOperations(num3, 4) << std::endl; // 2 + std::cout << Solution::maxOperations(num4, 2) << std::endl; // 0 + std::cout << Solution::maxOperations(num5, 3) << std::endl; // 4 + + return 0; +} -- cgit v1.2.3