-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestMultipleofThree.cpp
36 lines (33 loc) · 1.12 KB
/
largestMultipleofThree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Source: https://leetcode.com/problems/largest-multiple-of-three/
// Author: Miao Zhang
// Date: 2021-04-27
class Solution {
public:
string largestMultipleOfThree(vector<int>& digits) {
vector<int> cnt(10);
for (int d: digits) cnt[d]++;
const int r = accumulate(begin(digits), end(digits), 0) % 3;
vector<vector<int>> remainders{{0, 3, 6, 9}, {1, 4, 7}, {2, 5, 8}};
auto getNum = [&] (const vector<int>& ds) {
for (int d: ds) cnt[d]--;
string res;
for (int d = 9; d >= 1; d--) {
res.append(cnt[d], d + '0');
}
res.append(res.empty() ? min(1, cnt[0]) : cnt[0], '0');
return res;
};
if (r == 0) return getNum({});
for (int d: remainders[r]) {
if (cnt[d]) return getNum({d});
}
for (int d1: remainders[3 - r]) {
for (int d2: remainders[3 - r]) {
if ((d1 == d2 && cnt[d1] > 1) || (d1 != d2 && cnt[d1] && cnt[d2])) {
return getNum({d1, d2});
}
}
}
return "";
}
};