diff --git a/algorithm/math/CMakeLists.txt b/algorithm/math/CMakeLists.txt index 78df83d8..4549a1d2 100644 --- a/algorithm/math/CMakeLists.txt +++ b/algorithm/math/CMakeLists.txt @@ -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}) diff --git a/algorithm/math/leetcode_1363.cpp b/algorithm/math/leetcode_1363.cpp new file mode 100644 index 00000000..6239f690 --- /dev/null +++ b/algorithm/math/leetcode_1363.cpp @@ -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 + +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 &digits) { + const auto sums = std::accumulate(digits.begin(), digits.end(), 0); + if (sums % 3 == 0) { + string result{}; + vector sorted{digits}; + std::sort(sorted.begin(), sorted.end(), std::greater<>()); + for (const auto &digit: sorted) { + result += std::to_string(digit); + } + return removeLastZeros(result); + } + vector 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 &digits) { + const auto sums = std::accumulate(digits.begin(), digits.end(), 0); + std::array 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('0' + i)); + } + return removeLastZeros(result); +} + +} + +} diff --git a/algorithm/math/leetcode_1363_test.hpp b/algorithm/math/leetcode_1363_test.hpp new file mode 100644 index 00000000..fe324516 --- /dev/null +++ b/algorithm/math/leetcode_1363_test.hpp @@ -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 +#include +#include +#include + +namespace leetcode_1363 { + +namespace leetcode_1363 { +string largestMultipleOfThree(const vector &digits); +namespace optimize{ +string largestMultipleOfThree(const vector &digits); +} +} + +using Catch::Matchers::Equals; + +TEST_CASE("1-1 [test_1363]", "[test_1363]") { + const vector 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 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 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 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 +