-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
4 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters