From 90508c55bca8d5faee47f613985ff4f0293c18da Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 12 Feb 2025 10:34:12 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#467=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 LeetCode/2342._Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp diff --git a/LeetCode/2342._Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp b/LeetCode/2342._Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp new file mode 100644 index 0000000..77cd0b0 --- /dev/null +++ b/LeetCode/2342._Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp @@ -0,0 +1,41 @@ +// 자리수 합을 키로 가지는 map을 만들고 +// 가장 위에 있는 수를 key로 출력 + +class Solution { +public: + int maximumSum(vector& nums) { + map> m; + + for (int n : nums) { + + int digitSum = 0; + int temp = n; + while (temp != 0) { + digitSum += temp % 10; + temp = temp / 10; + } + + m[digitSum].push_back(n); + } + + int result = -1; + + for (auto &entry : m) { + list& valuse = entry.second; + + if (valuse.size() >= 2) { + + valuse.sort(greater()); + + auto it = valuse.begin(); + int first = *it; + ++it; + int second = *it; + + result = max(result, first + second); + } + } + + return result; + } +}; \ No newline at end of file