-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,7 @@ | |
"cxbc", | ||
"dbqca", | ||
"DCMAKE", | ||
"ddinggo", | ||
"dearmour", | ||
"deque", | ||
"dlimit", | ||
|
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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(); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-23 10:16:33 | ||
// 执行编译时间:2024-01-29 10:39:09 | ||
#include <gtest/gtest.h> | ||
#include <lib.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
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); | ||
} |