diff --git a/algorithm/math/CMakeLists.txt b/algorithm/math/CMakeLists.txt index d63cb226..88480b24 100644 --- a/algorithm/math/CMakeLists.txt +++ b/algorithm/math/CMakeLists.txt @@ -13,6 +13,7 @@ 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 1363 1390) +LIST(APPEND leetcode_order 1402) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/math/leetcode_1402.cpp b/algorithm/math/leetcode_1402.cpp new file mode 100644 index 00000000..f44803c3 --- /dev/null +++ b/algorithm/math/leetcode_1402.cpp @@ -0,0 +1,51 @@ + + +// SPDX-License-Identifier: AGPL-3.0-or-later +/* +CS203_DSAA_template + +Copyright (C) 2020-2023 nanoseeds + +*/ +#include "leetcode_1402_test.hpp" +#include + +namespace leetcode_1402 { + +namespace leetcode_1402 { +int32_t maxSatisfaction(const vector &satisfaction) { + vector vec{}; + std::map> map{}; + for (const auto x: satisfaction) { + if (x >= 0) { + vec.push_back(x); + } else { + map[x]++; // 又或者可以从中心向两边分别遍历 + } + } + std::sort(vec.begin(), vec.end(), [](const auto a, const auto b) { return a < b; }); + const auto size{vec.size()}; + int32_t mul1{0},sum1{0}; + for (size_t i{size}; i > 0; i--) { + mul1 = mul1 + (vec[i - 1]) * static_cast(i); + sum1 = sum1 + (vec[i - 1]) ; + }// 后面排序根本就不会变化 + // 只有前面会变化, 但是排序是从小到大固定的, 问题只是到哪里停罢了 + for (const auto &[k, v]: map) { + for (int32_t i{0}; i < v; i++) { + const auto diff = k + sum1; + if (diff > 0) { + mul1 += diff; + sum1 += k; + } else { + break; + } + } + } + return mul1; +} + +} + + +} diff --git a/algorithm/math/leetcode_1402_test.hpp b/algorithm/math/leetcode_1402_test.hpp new file mode 100644 index 00000000..adc8e3a0 --- /dev/null +++ b/algorithm/math/leetcode_1402_test.hpp @@ -0,0 +1,64 @@ + + +// SPDX-License-Identifier: AGPL-3.0-or-later +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag 数学 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1402_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1402_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_1402 { + +namespace leetcode_1402 { +int32_t maxSatisfaction(const vector &satisfaction); +} + +using Catch::Matchers::Equals; + +TEST_CASE("1-1 [test_1402]", "[test_1402]") { + const vector input{1, 1, 4, 5, 1, 4}; + constexpr const auto result{72}; + CHECK(result == leetcode_1402::maxSatisfaction(input)); +} + +TEST_CASE("1-2 [test_1402]", "[test_1402]") { + const vector input{21, 4, 7}; + constexpr const auto result{81}; + CHECK(result == leetcode_1402::maxSatisfaction(input)); +} + +TEST_CASE("1-3 [test_1402]", "[test_1402]") { + const vector input{-1, 0, 5, -8, -9}; + constexpr const auto result{14}; + CHECK(result == leetcode_1402::maxSatisfaction(input)); +} + +TEST_CASE("1-4 [test_1402]", "[test_1402]") { + const vector input{2, 4, 3}; + constexpr const auto result{20}; + CHECK(result == leetcode_1402::maxSatisfaction(input)); +} + +TEST_CASE("1-5 [test_1402]", "[test_1402]") { + const vector input{-1, -4, -5}; + constexpr const auto result{0}; + CHECK(result == leetcode_1402::maxSatisfaction(input)); +} + +TEST_CASE("1-6 [test_1402]", "[test_1402]") { + const vector input{2, -2, -3, 1}; + constexpr const auto result{6}; + CHECK(result == leetcode_1402::maxSatisfaction(input)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1402_TEST_HPP + diff --git a/algorithm/string/leetcode_1392.cpp b/algorithm/string/leetcode_1392.cpp index 680b660c..1d56b27b 100644 --- a/algorithm/string/leetcode_1392.cpp +++ b/algorithm/string/leetcode_1392.cpp @@ -27,11 +27,11 @@ string longestPrefix(const string &s) { } namespace hash { string longestPrefix(const string &s) { - int32_t count{0}; + size_t count{0}; constexpr const auto prime{1000'000'007},prime2{1000'000'009}; size_t hashPrefix{0}, hashPostfix{0}, hashMultiply{1}; size_t hashPrefix2{0}, hashPostfix2{0}, hashMultiply2{1}; - for (int32_t i{0}; i < s.size() - 1; i++) { + for (size_t i{0}; i < s.size() - 1; i++) { hashPrefix = (hashPrefix * 26 + s[i]) % prime; hashPrefix2 = (hashPrefix2 * 26 + s[i]) % prime2; hashPostfix = (hashPostfix + (s[s.size() - 1 - i]) * hashMultiply) % prime;