Skip to content

Commit 160b01c

Browse files
committed
feat: 完成 dp 目录重构
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
1 parent b135836 commit 160b01c

118 files changed

Lines changed: 2973 additions & 2418 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

algorithm/dp/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ enable_testing()
99
set(dependencies package divide_bar matrix_multiply)
1010

1111
set(leetcode_order 1143 5 10 44 45)
12-
LIST(APPEND leetcode_order 63 62 64 70_746)
13-
LIST(APPEND leetcode_order 97 322 509_1137 416)
14-
LIST(APPEND leetcode_order 198_213 740 55 918)
12+
LIST(APPEND leetcode_order 63 62 64 70 746)
13+
LIST(APPEND leetcode_order 97 322 509 1137 416)
14+
LIST(APPEND leetcode_order 198 213 740 55 918)
1515
LIST(APPEND leetcode_order 152 120 1567 1014 121)
1616
LIST(APPEND leetcode_order 122 309 714 139 413)
17-
LIST(APPEND leetcode_order 91 264 931 304_1314 221)
17+
LIST(APPEND leetcode_order 91 264 931 304 1314 221)
1818
LIST(APPEND leetcode_order 516 300 174 376 643)
1919
LIST(APPEND leetcode_order 392 72 518 377 279)
2020
LIST(APPEND leetcode_order so_46 435 1025 1044 1277)
@@ -26,10 +26,10 @@ unset(leetcode_order)
2626

2727

2828
foreach (elementName IN LISTS dependencies)
29-
add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}.cpp)
29+
add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}_test.cpp)
3030
target_link_libraries(${PROJECT_NAME}_${elementName} CS203_DSAA_template_INCLUDE)
3131
target_compile_definitions(${PROJECT_NAME}_${elementName} PRIVATE CS203_DSAA_TEST_MACRO)
32-
MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}.cpp")
32+
MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}_test.cpp")
3333
add_test(${PROJECT_NAME}_${elementName}_CTEST ${PROJECT_NAME}_${elementName})
3434
endforeach ()
3535
unset(dependencies)

algorithm/dp/divide_bar.cpp

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,77 @@ CS203_DSAA_template
55
Copyright (C) 2020-2023 nanos
66
77
*/
8-
#include "divide_bar_test.hpp"
98

109
#include <cstddef>
1110
#include <cstdint>
1211
#include <vector>
13-
12+
#include <algorithm>
13+
#include <tuple>
1414

1515
namespace dp {
1616
namespace divide_bar { // Introduction to Algorithms Edition3 Chapter15.1
17+
1718
namespace iter {
18-
int64_t maxProfit(const vector<int64_t> &divide_profits, size_t length) {
19-
const auto minIter{std::min(divide_profits.size(), length)};
20-
vector<int64_t> profits_table(length + 1, -0x3f3f3f);
21-
profits_table[0] = 0;
22-
for (size_t i{0}; i < minIter; ++i) {
23-
profits_table[i + 1] = divide_profits[i];
24-
}
25-
for (size_t i{2}; i <= length; ++i) {
26-
for (size_t j{1}; j < (i / 2 + 1); ++j) {
27-
profits_table[i] = std::max(profits_table[i], profits_table[j] + profits_table[i - j]);
19+
class Solution {
20+
public:
21+
int64_t maxProfit(const std::vector<int64_t> &divide_profits, size_t length) {
22+
const auto minIter{std::min(divide_profits.size(), length)};
23+
std::vector<int64_t> profits_table(length + 1, -0x3f3f3f);
24+
profits_table[0] = 0;
25+
for (size_t i{0}; i < minIter; ++i) {
26+
profits_table[i + 1] = divide_profits[i];
2827
}
28+
for (size_t i{2}; i <= length; ++i) {
29+
for (size_t j{1}; j < (i / 2 + 1); ++j) {
30+
profits_table[i] = std::max(profits_table[i], profits_table[j] + profits_table[i - j]);
31+
}
32+
}
33+
return profits_table.back();
2934
}
30-
return profits_table.back();
35+
};
3136
}
3237

3338
namespace with_solution {
34-
std::tuple<int64_t, vector<int64_t>> maxProfit(const vector<int64_t> &divide_profits, size_t length) {
35-
const auto minIter{std::min(divide_profits.size(), length)};
36-
vector<int64_t> profits_table(length + 1, -0x3f3f3f);
37-
vector<vector<int64_t>> segment_table(length + 1);
38-
profits_table[0] = 0;
39-
segment_table[0] = {};
40-
for (size_t i{0}; i < minIter; ++i) {
41-
profits_table[i + 1] = divide_profits[i];
42-
segment_table[i + 1].push_back(static_cast<int64_t>(i + 1));
43-
}
44-
for (size_t i{2}; i <= length; ++i) {
45-
size_t left{0}, right{0};
46-
for (size_t j{1}; j < (i / 2 + 1); ++j) {
47-
const auto sums = profits_table[j] + profits_table[i - j];
48-
if (sums > profits_table[i]) { // 相对倾向于保留大块
49-
profits_table[i] = sums;
50-
left = j;
51-
right = i - j;
52-
}
39+
class Solution {
40+
public:
41+
std::tuple<int64_t, std::vector<int64_t>> maxProfit(const std::vector<int64_t> &divide_profits, size_t length) {
42+
const auto minIter{std::min(divide_profits.size(), length)};
43+
std::vector<int64_t> profits_table(length + 1, -0x3f3f3f);
44+
std::vector<std::vector<int64_t>> segment_table(length + 1);
45+
profits_table[0] = 0;
46+
segment_table[0] = {};
47+
for (size_t i{0}; i < minIter; ++i) {
48+
profits_table[i + 1] = divide_profits[i];
49+
segment_table[i + 1].push_back(static_cast<int64_t>(i + 1));
5350
}
54-
if (left != 0 && right != 0) {
55-
auto mid = segment_table[left];
56-
mid.insert(mid.end(), segment_table[right].cbegin(), segment_table[right].cend());
57-
segment_table[i] = mid; // 操作完成之后赋值,防止操作自己
51+
for (size_t i{2}; i <= length; ++i) {
52+
size_t left{0}, right{0};
53+
for (size_t j{1}; j < (i / 2 + 1); ++j) {
54+
const auto sums = profits_table[j] + profits_table[i - j];
55+
if (sums > profits_table[i]) { // 相对倾向于保留大块
56+
profits_table[i] = sums;
57+
left = j;
58+
right = i - j;
59+
}
60+
}
61+
if (left != 0 && right != 0) {
62+
auto mid = segment_table[left];
63+
mid.insert(mid.end(), segment_table[right].cbegin(), segment_table[right].cend());
64+
segment_table[i] = mid; // 操作完成之后赋值,防止操作自己
65+
}
5866
}
67+
return std::make_tuple(profits_table.back(), segment_table.back());
5968
}
60-
return std::make_tuple(profits_table.back(), segment_table.back());
61-
}
69+
};
6270
}
6371

64-
}
6572
namespace rec {
66-
int64_t maxProfit(const vector<int64_t> &divide_profits, size_t length) {
67-
return 0;
68-
}
73+
class Solution {
74+
public:
75+
int64_t maxProfit(const std::vector<int64_t> &divide_profits, size_t length) {
76+
return 0;
77+
}
78+
};
6979
}
7080

7181
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@ Copyright (C) 2020-2023 nanos
99
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_DP_DIVIDE_BAR_TEST_HPP
1010
#define CS203_DSAA_TEMPLATE_ALGORITHM_DP_DIVIDE_BAR_TEST_HPP
1111

12-
#include "package.hpp"
1312
#include <catch_main.hpp>
13+
#include "divide_bar.cpp"
1414

1515
namespace dp {
1616
namespace divide_bar {
17+
using std::vector;
18+
1719
namespace iter {
18-
int64_t maxProfit(const vector<int64_t> &divide_profits, size_t length);
1920

2021
TEST_CASE("1 [test_divide_bar]", "[test_divide_bar]") {
2122
const vector<int64_t> profits{1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
2223
const vector<int64_t> results{0, 1, 5, 8, 10, 13, 17, 18, 22, 25, 30};
2324
const auto maxLength{results.size()};
25+
Solution solution;
2426
for (size_t i{0}; i < maxLength; ++i) {
25-
CHECK(results[i] == maxProfit(profits, i));
27+
CHECK(results[i] == solution.maxProfit(profits, i));
2628
}
2729
}
30+
}
2831

2932
namespace with_solution {
30-
std::tuple<int64_t, vector<int64_t>> maxProfit(const vector<int64_t> &divide_profits, size_t length);
3133

3234
using Catch::Matchers::Equals;
3335

@@ -47,20 +49,17 @@ TEST_CASE("1 [test_divide_bar::iter::with_solution]", "[test_divide_bar]") {
4749
std::make_tuple(30, vector<int64_t>{10}), // instead of {2,2,6}
4850
};
4951
const auto maxLength{results.size()};
52+
Solution solution;
5053
for (size_t i{0}; i < maxLength; ++i) {
5154
const auto [resultProfit, resultNums] = results[i];
52-
const auto [outputProfit, outputNums] = maxProfit(profits, i);
55+
const auto [outputProfit, outputNums] = solution.maxProfit(profits, i);
5356
CHECK(resultProfit == outputProfit);
5457
CHECK_THAT(resultNums, Equals(outputNums));
5558
}
5659
}
5760
}
58-
}
5961

60-
namespace rec {
61-
int64_t maxProfit(const vector<int64_t> &divide_profits, size_t length);
62-
63-
}
6462
}
6563
}
64+
6665
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_DP_DIVIDE_BAR_TEST_HPP

algorithm/dp/leetcode_10.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,45 @@ CS203_DSAA_template
55
Copyright (C) 2020-2023 nanos
66
77
*/
8-
#include "leetcode_10_test.hpp"
8+
99
#include <vector>
10+
#include <string>
11+
#include <cstdint>
1012

11-
namespace lcs_10 {
13+
#ifdef CS203_DSAA_TEST_MACRO
14+
namespace leetcode_10 {
1215
using std::vector;
16+
using std::string;
17+
#endif
1318

14-
bool leetcode_10::isMatch(const string &s, const string &p) {
15-
static constexpr const auto func = [](const auto x, const auto y) {
16-
return y == '.' || x == y;
17-
};
18-
const auto s_size{s.size()}, p_size{p.size()};
19-
vector<vector<uint8_t>> DP(s_size + 1, vector<uint8_t>(p_size + 1, false));
20-
DP[0][0] = true;
21-
for (size_t j{1}; j <= p_size; ++j) {
22-
DP[0][j] = (p[j - 1] == '*' && DP[0][j - 2]);
23-
}
24-
for (size_t i{1}; i <= s_size; ++i) {
25-
const auto s_char{s[i - 1]};
19+
class Solution {
20+
public:
21+
bool isMatch(const string &s, const string &p) {
22+
static constexpr const auto func = [](const auto x, const auto y) {
23+
return y == '.' || x == y;
24+
};
25+
const auto s_size{s.size()}, p_size{p.size()};
26+
vector<vector<uint8_t>> DP(s_size + 1, vector<uint8_t>(p_size + 1, false));
27+
DP[0][0] = true;
2628
for (size_t j{1}; j <= p_size; ++j) {
27-
const auto p_char{p[j - 1]};
28-
if (p[j - 1] == '*') { // p[0], j =1 will not appear '*'
29-
const auto p_last_char{p[j - 2]};
30-
DP[i][j] = DP[i][j - 2] || (func(s_char, p_last_char) && DP[i - 1][j]);
31-
} else if (func(s_char, p_char)) {
32-
DP[i][j] = DP[i - 1][j - 1];
29+
DP[0][j] = (p[j - 1] == '*' && DP[0][j - 2]);
30+
}
31+
for (size_t i{1}; i <= s_size; ++i) {
32+
const auto s_char{s[i - 1]};
33+
for (size_t j{1}; j <= p_size; ++j) {
34+
const auto p_char{p[j - 1]};
35+
if (p[j - 1] == '*') { // p[0], j =1 will not appear '*'
36+
const auto p_last_char{p[j - 2]};
37+
DP[i][j] = DP[i][j - 2] || (func(s_char, p_last_char) && DP[i - 1][j]);
38+
} else if (func(s_char, p_char)) {
39+
DP[i][j] = DP[i - 1][j - 1];
40+
}
3341
}
3442
}
43+
return DP.back().back();
3544
}
36-
return DP.back().back();
37-
}
45+
};
46+
47+
#ifdef CS203_DSAA_TEST_MACRO
3848
}
49+
#endif

algorithm/dp/leetcode_1014.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,45 @@ CS203_DSAA_template
55
Copyright (C) 2020-2023 nanos
66
77
*/
8-
#include "leetcode_1014_test.hpp"
98

9+
#include <vector>
10+
#include <algorithm>
11+
#include <cstdint>
12+
13+
#ifdef CS203_DSAA_TEST_MACRO
1014
namespace leetcode_1014 {
15+
using std::vector;
16+
#endif
1117

12-
int32_t leetcode_1014::maxScoreSightseeingPair(const vector<int32_t> &nums) noexcept {
13-
const int32_t nums_size = nums.size();
14-
if (nums_size <= 1) {
15-
return 0x3f3f3f3f;
16-
}
17-
vector<int32_t> dp(nums_size + 1, 0);
18-
// dp[i] => 前个里哪一个充当 values[i]+i里的i
19-
dp[0] = 0; // can not match pair
20-
dp[1] = 0; // 还没成对
21-
dp[2] = 0; // 第二个(1th)之前的i序号为0
22-
int32_t maxDiff{nums[0] + 0 + nums[1] - 1};
23-
for (int32_t j{3}; j <= nums_size; j++) {
24-
const auto i = dp[j - 1];
25-
const int32_t base = nums[j - 1] - j + 1;
26-
const int32_t diff1 = base + nums[i] + i;
27-
const int32_t diff2 = base + nums[j - 2] + j - 2;
28-
if (diff2 > diff1) {
29-
dp[j] = j - 2;
30-
} else {
31-
dp[j] = i;
18+
class Solution {
19+
public:
20+
int32_t maxScoreSightseeingPair(const vector<int32_t> &nums) noexcept {
21+
const int32_t nums_size = nums.size();
22+
if (nums_size <= 1) {
23+
return 0x3f3f3f3f;
3224
}
33-
maxDiff = std::max({maxDiff, diff1, diff2});
25+
vector<int32_t> dp(nums_size + 1, 0);
26+
// dp[i] => 前个里哪一个充当 values[i]+i里的i
27+
dp[0] = 0; // can not match pair
28+
dp[1] = 0; // 还没成对
29+
dp[2] = 0; // 第二个(1th)之前的i序号为0
30+
int32_t maxDiff{nums[0] + 0 + nums[1] - 1};
31+
for (int32_t j{3}; j <= nums_size; j++) {
32+
const auto i = dp[j - 1];
33+
const int32_t base = nums[j - 1] - j + 1;
34+
const int32_t diff1 = base + nums[i] + i;
35+
const int32_t diff2 = base + nums[j - 2] + j - 2;
36+
if (diff2 > diff1) {
37+
dp[j] = j - 2;
38+
} else {
39+
dp[j] = i;
40+
}
41+
maxDiff = std::max({maxDiff, diff1, diff2});
42+
}
43+
return maxDiff;
3444
}
35-
return maxDiff;
36-
}
45+
};
3746

47+
#ifdef CS203_DSAA_TEST_MACRO
3848
}
49+
#endif
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@ Copyright (C) 2020-2023 nanos
1313
#define CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1014_TEST_CPP
1414

1515
#include <catch_main.hpp>
16-
#include <cstdint>
17-
#include <cstddef>
18-
#include <vector>
16+
#include "leetcode_1014.cpp"
1917

2018
namespace leetcode_1014 {
2119
using std::vector;
2220

23-
struct leetcode_1014 {
24-
static int32_t maxScoreSightseeingPair(const vector<int32_t> &values) noexcept;
25-
};
26-
2721
TEST_CASE("3 [test_1014]", "[test_1014]") {
2822
const vector<int32_t> input{4, 7, 5, 8};
2923
static constexpr const auto result{13};
30-
CHECK(result == leetcode_1014::maxScoreSightseeingPair(input));
24+
Solution solution;
25+
CHECK(result == solution.maxScoreSightseeingPair(input));
3126
}
3227

3328
}

0 commit comments

Comments
 (0)