Skip to content

Commit

Permalink
add: 自由之路
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 29, 2024
1 parent e632bc4 commit a841068
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
"cxbc",
"dbqca",
"DCMAKE",
"ddinggo",
"dearmour",
"deque",
"dlimit",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

### 其它

- [自由之路](src/search/freedom_trail.cpp) [深度优先搜索, 广度优先搜索, 字符串, 动态规划]

- LeetCode 514. 自由之路 <https://leetcode.cn/problems/freedom-trail>

- [构造有效字符串的最少插入数](src/other/minimum_additions_to_make_valid_string.cpp) [Stack, Greedy, String, Dynamic Programming]

- LeetCode 2645. 构造有效字符串的最少插入数 <https://leetcode.cn/problems/minimum-additions-to-make-valid-string>
Expand Down
Binary file added images/search/freedom_trail.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/search/freedom_trail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 自由之路
// https://leetcode.cn/problems/freedom-trail
// INLINE ../../images/search/freedom_trail.jpeg

#include <headers.hpp>

class Solution {
public:
int findRotateSteps(string ring, string key) {
array<vector<int>, 26> idx_list;
int n = ring.size(), m = key.size(), pc = ring.front() - 'a';
for (int i = 0; i < n; ++i)
idx_list[ring[i] - 'a'].push_back(i);
vector<int> dp(n, 0x3f3f3f3f), pre(n, 0x3f3f3f3f);
pre[0] = 0;
for (int i = 0; i < m; ++i) {
for (int idx : idx_list[key[i] - 'a'])
for (int pi : idx_list[pc])
dp[idx] =
min(dp[idx], pre[pi] + min(abs(pi - idx), n - abs(pi - idx)));
pre = std::move(dp);
dp = vector(n, 0x3f3f3f3f);
pc = key[i] - 'a';
}
return *min_element(pre.begin(), pre.end()) + key.size();
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-23 10:16:33
// 执行编译时间:2024-01-29 10:39:09
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down
22 changes: 22 additions & 0 deletions test/search/freedom_trail_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <search/freedom_trail.cpp>

TEST(自由之路, findRotateSteps) {
Solution solution;
// 示例 1:
// 输入: ring = "godding", key = "gd"
// 输出: 4
// 解释:
// 对于 key 的第一个字符 'g',已经在正确的位置, 我们只需要1步来拼写这个字符。
// 对于 key 的第二个字符 'd',我们需要逆时针旋转 ring "godding" 2步使它变成
// "ddinggo"。 当然, 我们还需要1步进行拼写。 因此最终的输出是 4。
string ring = "godding";
string key = "gd";
EXPECT_EQ(solution.findRotateSteps(ring, key), 4);

// 示例 2:
// 输入: ring = "godding", key = "godding"
// 输出: 13
ring = "godding";
key = "godding";
EXPECT_EQ(solution.findRotateSteps(ring, key), 13);
}

0 comments on commit a841068

Please sign in to comment.