Skip to content

Commit

Permalink
이슈 #467에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 12, 2025
1 parent b117165 commit 90508c5
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions LeetCode/2342._Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 자리수 합을 키로 가지는 map을 만들고
// 가장 위에 있는 수를 key로 출력

class Solution {
public:
int maximumSum(vector<int>& nums) {
map<int, list<int>> 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<int>& valuse = entry.second;

if (valuse.size() >= 2) {

valuse.sort(greater<int>());

auto it = valuse.begin();
int first = *it;
++it;
int second = *it;

result = max(result, first + second);
}
}

return result;
}
};

0 comments on commit 90508c5

Please sign in to comment.