Skip to content

Commit e3ff408

Browse files
authored
Update largest-multiple-of-three.cpp
1 parent 425782f commit e3ff408

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

C++/largest-multiple-of-three.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Solution {
1010
{2, {{2}, {5}, {8}, {1, 1}, {1, 4}, {4, 4}, {1, 7}, {4, 7}, {7, 7}}}
1111
};
1212
unordered_map<int, int> count = counter(digits);
13-
const auto& total = accumulate(cbegin(digits), cend(digits), 0);
14-
for (const auto& deletes : lookup.at(total % 3)) {
13+
const auto& r = accumulate(cbegin(digits), cend(digits), 0) % 3;
14+
for (const auto& deletes : lookup.at(r)) {
1515
if (remove(deletes, &count)) {
1616
break;
1717
}
@@ -53,12 +53,12 @@ class Solution2 {
5353
public:
5454
string largestMultipleOfThree(vector<int>& digits) {
5555
unordered_map<int, int> count = counter(digits);
56-
const auto& total = accumulate(cbegin(digits), cend(digits), 0);
57-
if (total % 3) {
56+
const auto& r = accumulate(cbegin(digits), cend(digits), 0) % 3;
57+
if (r) {
5858
bool is_found = false;
5959
for (int i = 0; i < 10; ++i) {
6060
vector<int> deletes = {i};
61-
if (remove(deletes, total, &count)) {
61+
if (remove(deletes, r, &count)) {
6262
is_found = true;
6363
break;
6464
}
@@ -67,7 +67,7 @@ class Solution2 {
6767
for (int i = 0; i < 10; ++i) {
6868
for (int j = 0; j <= i; ++j) {
6969
vector<int> deletes = {i, j};
70-
if (remove(deletes, total, &count)) {
70+
if (remove(deletes, r, &count)) {
7171
break;
7272
}
7373
}
@@ -82,9 +82,9 @@ class Solution2 {
8282
}
8383

8484
private:
85-
bool remove(const vector<int>& deletes, int total, unordered_map<int, int> *count) {
85+
bool remove(const vector<int>& deletes, int r, unordered_map<int, int> *count) {
8686
const auto& delete_count = counter(deletes);
87-
if (accumulate(cbegin(deletes), cend(deletes), 0) % 3 == total % 3 &&
87+
if (accumulate(cbegin(deletes), cend(deletes), 0) % 3 == r &&
8888
all_of(cbegin(delete_count), cend(delete_count),
8989
[&count](const auto& kvp) {
9090
return (*count)[kvp.first] >= kvp.second;

0 commit comments

Comments
 (0)