Skip to content

Commit

Permalink
feat: update lc-1363
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Aug 28, 2023
1 parent 0aecab6 commit 964f968
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
2 changes: 1 addition & 1 deletion algorithm/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LIST(APPEND leetcode_order 118_119 169 217 263)
LIST(APPEND leetcode_order 268 283 338 343 372)
LIST(APPEND leetcode_order 401 414 461 728 781)
LIST(APPEND leetcode_order 136 75 883 1018 1185)
LIST(APPEND leetcode_order 670 1252 1362)
LIST(APPEND leetcode_order 670 1252 1362 1363)
LIST(TRANSFORM leetcode_order PREPEND leetcode_)

set(dependencies ${dependencies} ${leetcode_order})
Expand Down
155 changes: 155 additions & 0 deletions algorithm/math/leetcode_1363.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@

// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2020-2023 nanoseeds
*/
#include "leetcode_1363_test.hpp"

#include <numeric>

namespace leetcode_1363 {
inline string removeLastZeros(const string &input) {
if (input.empty()) {
return input;
}
const auto length{input.length()};
size_t pre_zeros{0};
for (size_t i{0}; i < length; i++) {
if (input[i] == '0') {
pre_zeros += 1;
} else {
break;
}
}
if (pre_zeros == length) {
return "0";
}
const auto will_return{input.substr(pre_zeros, length - pre_zeros)};
return will_return;
}

string leetcode_1363::largestMultipleOfThree(const vector<int32_t> &digits) {
const auto sums = std::accumulate(digits.begin(), digits.end(), 0);
if (sums % 3 == 0) {
string result{};
vector<int32_t> sorted{digits};
std::sort(sorted.begin(), sorted.end(), std::greater<>());
for (const auto &digit: sorted) {
result += std::to_string(digit);
}
return removeLastZeros(result);
}
vector<int32_t> triples{}, triples_plusone{}, triples_plustwo{};
for (const auto &digit: digits) {
if (digit % 3 == 0) {
triples.push_back(digit);
} else if (digit % 3 == 1) {
triples_plusone.push_back(digit);
} else {
triples_plustwo.push_back(digit);
}
}
std::sort(triples_plusone.begin(), triples_plusone.end(), std::greater<>());
std::sort(triples_plustwo.begin(), triples_plustwo.end(), std::greater<>());

if (sums % 3 == 1) {
if (triples_plusone.empty()) {
if (triples_plustwo.size() < 2) {
return {};
}
triples_plustwo.pop_back();
triples_plustwo.pop_back();
} else {
triples_plusone.pop_back();
}
for (const auto plusone: triples_plusone) {
triples.push_back(plusone);
}
for (const auto plustwo: triples_plustwo) {
triples.push_back(plustwo);
}
std::sort(triples.begin(), triples.end(), std::greater<>());
string result{};
for (const auto &digit: triples) {
result += std::to_string(digit);
}
return removeLastZeros(result);
} else if (sums % 3 == 2) {
if (triples_plustwo.empty()) {
if (triples_plusone.size() < 2) {
return {};
}
triples_plusone.pop_back();
triples_plusone.pop_back();
} else {
triples_plustwo.pop_back();
}
for (const auto plusone: triples_plusone) {
triples.push_back(plusone);
}
for (const auto plustwo: triples_plustwo) {
triples.push_back(plustwo);
}
std::sort(triples.begin(), triples.end(), std::greater<>());
string result{};
for (const auto &digit: triples) {
result += std::to_string(digit);
}
return removeLastZeros(result);
}
return {};
}

namespace leetcode_1363::optimize {
string largestMultipleOfThree(const vector<int32_t> &digits) {
const auto sums = std::accumulate(digits.begin(), digits.end(), 0);
std::array<int32_t, 10> cnt{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (const auto &digit: digits) {
cnt[digit] += 1;
}
if (sums % 3 == 1) {
if ((cnt[1] + cnt[4] + cnt[7]) >= 1) {
for (const auto num: {1, 4, 7}) {
cnt[num] -= 1;
break;
}
} else if ((cnt[2] + cnt[5] + cnt[8]) >= 2) {
for (uint8_t x{0}; x < 2u; x++) {
for (const auto num: {2, 5, 8}) {
cnt[num] -= 1;
break;
}
}
} else {
return {};
}
} else if (sums % 3 == 2) {
if ((cnt[2] + cnt[5] + cnt[8]) >= 1) {
for (const auto num: {2, 5, 8}) {
cnt[num] -= 1;
break;
}
} else if ((cnt[1] + cnt[4] + cnt[7]) >= 2) {
for (uint8_t x{0}; x < 2u; x++) {
for (const auto num: {1, 4, 7}) {
cnt[num] -= 1;
break;
}
}
} else {
return {};
}
}
string result{};
for (int32_t i{9}; i >= 0; i--) {
result += std::string(cnt[i], static_cast<char>('0' + i));
}
return removeLastZeros(result);
}

}

}
58 changes: 58 additions & 0 deletions algorithm/math/leetcode_1363_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2022 nanoseeds
*/
//@Tag 数学
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1363_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1363_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>

namespace leetcode_1363 {

namespace leetcode_1363 {
string largestMultipleOfThree(const vector<int32_t> &digits);
namespace optimize{
string largestMultipleOfThree(const vector<int32_t> &digits);
}
}

using Catch::Matchers::Equals;

TEST_CASE("1-1 [test_1363]", "[test_1363]") {
const vector<int32_t> input{8, 1, 9};
constexpr const char *const result{"981"};
CHECK(result == leetcode_1363::largestMultipleOfThree(input));
CHECK(result == leetcode_1363::optimize::largestMultipleOfThree(input));
}

TEST_CASE("1-2 [test_1363]", "[test_1363]") {
const vector<int32_t> input{8, 6, 7, 1, 0};
constexpr const char *const result{"8760"};
CHECK(result == leetcode_1363::largestMultipleOfThree(input));
CHECK(result == leetcode_1363::optimize::largestMultipleOfThree(input));
}

TEST_CASE("1-3 [test_1363]", "[test_1363]") {
const vector<int32_t> input{1};
constexpr const char *const result{""};
CHECK(result == leetcode_1363::largestMultipleOfThree(input));
CHECK(result == leetcode_1363::optimize::largestMultipleOfThree(input));
}

TEST_CASE("1-4 [test_1363]", "[test_1363]") {
const vector<int32_t> input{0, 0, 0, 0, 0, 0};
constexpr const char *const result{"0"};
CHECK(result == leetcode_1363::largestMultipleOfThree(input));
CHECK(result == leetcode_1363::optimize::largestMultipleOfThree(input));
}
}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1363_TEST_HPP

0 comments on commit 964f968

Please sign in to comment.