Skip to content

Commit

Permalink
feat: update lc-1402
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Sep 14, 2023
1 parent 36d6af1 commit e470012
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions algorithm/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
51 changes: 51 additions & 0 deletions algorithm/math/leetcode_1402.cpp
Original file line number Diff line number Diff line change
@@ -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 <map>

namespace leetcode_1402 {

namespace leetcode_1402 {
int32_t maxSatisfaction(const vector<int32_t> &satisfaction) {
vector<int32_t> vec{};
std::map<int32_t, int32_t, std::greater<>> 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<int32_t>(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;
}

}


}
64 changes: 64 additions & 0 deletions algorithm/math/leetcode_1402_test.hpp
Original file line number Diff line number Diff line change
@@ -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 <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>

namespace leetcode_1402 {

namespace leetcode_1402 {
int32_t maxSatisfaction(const vector<int32_t> &satisfaction);
}

using Catch::Matchers::Equals;

TEST_CASE("1-1 [test_1402]", "[test_1402]") {
const vector<int32_t> 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<int32_t> 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<int32_t> 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<int32_t> 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<int32_t> 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<int32_t> 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

4 changes: 2 additions & 2 deletions algorithm/string/leetcode_1392.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e470012

Please sign in to comment.