-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b117165
commit 90508c5
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
LeetCode/2342._Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |