Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff3350d

Browse files
authored
feat: add ts solution to lc problem: No.0514 (doocs#2273)
No.0514.Freedom Trail
1 parent e3f7ac0 commit ff3350d

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

solution/0500-0599/0514.Freedom Trail/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,40 @@ func abs(x int) int {
194194
}
195195
```
196196

197+
```ts
198+
function findRotateSteps(ring: string, key: string): number {
199+
const m: number = key.length;
200+
const n: number = ring.length;
201+
const pos: number[][] = Array.from({ length: 26 }, () => []);
202+
for (let i = 0; i < n; ++i) {
203+
const j: number = ring.charCodeAt(i) - 'a'.charCodeAt(0);
204+
pos[j].push(i);
205+
}
206+
207+
const f: number[][] = Array.from({ length: m }, () => Array(n).fill(1 << 30));
208+
for (const j of pos[key.charCodeAt(0) - 'a'.charCodeAt(0)]) {
209+
f[0][j] = Math.min(j, n - j) + 1;
210+
}
211+
212+
for (let i = 1; i < m; ++i) {
213+
for (const j of pos[key.charCodeAt(i) - 'a'.charCodeAt(0)]) {
214+
for (const k of pos[key.charCodeAt(i - 1) - 'a'.charCodeAt(0)]) {
215+
f[i][j] = Math.min(
216+
f[i][j],
217+
f[i - 1][k] + Math.min(Math.abs(j - k), n - Math.abs(j - k)) + 1,
218+
);
219+
}
220+
}
221+
}
222+
223+
let ans: number = 1 << 30;
224+
for (const j of pos[key.charCodeAt(m - 1) - 'a'.charCodeAt(0)]) {
225+
ans = Math.min(ans, f[m - 1][j]);
226+
}
227+
return ans;
228+
}
229+
```
230+
197231
<!-- tabs:end -->
198232

199233
<!-- end -->

solution/0500-0599/0514.Freedom Trail/README_EN.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,19 @@ So the final output is 4.
4848

4949
## Solutions
5050

51-
### Solution 1
51+
### Solution 1: Dynamic Programming
52+
53+
First, we preprocess the positions of each character $c$ in the string $ring$, and record them in the array $pos[c]$. Suppose the lengths of the strings $key$ and $ring$ are $m$ and $n$, respectively.
54+
55+
Then we define $f[i][j]$ as the minimum number of steps to spell the first $i+1$ characters of the string $key$, and the $j$-th character of $ring$ is aligned with the $12:00$ direction. Initially, $f[i][j]=+\infty$. The answer is $\min_{0 \leq j < n} f[m - 1][j]$.
56+
57+
We can first initialize $f[0][j]$, where $j$ is the position where the character $key[0]$ appears in $ring$. Since the $j$-th character of $ring$ is aligned with the $12:00$ direction, we only need $1$ step to spell $key[0]$. In addition, we need $min(j, n - j)$ steps to rotate $ring$ to the $12:00$ direction. Therefore, $f[0][j]=min(j, n - j) + 1$.
58+
59+
Next, we consider how the state transitions when $i \geq 1$. We can enumerate the position list $pos[key[i]]$ where $key[i]$ appears in $ring$, and enumerate the position list $pos[key[i-1]]$ where $key[i-1]$ appears in $ring$, and then update $f[i][j]$, i.e., $f[i][j]=\min_{k \in pos[key[i-1]]} f[i-1][k] + \min(\text{abs}(j - k), n - \text{abs}(j - k)) + 1$.
60+
61+
Finally, we return $\min_{0 \leq j \lt n} f[m - 1][j]$.
62+
63+
The time complexity is $O(m \times n^2)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the lengths of the strings $key$ and $ring$, respectively.
5264

5365
<!-- tabs:start -->
5466

@@ -174,6 +186,40 @@ func abs(x int) int {
174186
}
175187
```
176188

189+
```ts
190+
function findRotateSteps(ring: string, key: string): number {
191+
const m: number = key.length;
192+
const n: number = ring.length;
193+
const pos: number[][] = Array.from({ length: 26 }, () => []);
194+
for (let i = 0; i < n; ++i) {
195+
const j: number = ring.charCodeAt(i) - 'a'.charCodeAt(0);
196+
pos[j].push(i);
197+
}
198+
199+
const f: number[][] = Array.from({ length: m }, () => Array(n).fill(1 << 30));
200+
for (const j of pos[key.charCodeAt(0) - 'a'.charCodeAt(0)]) {
201+
f[0][j] = Math.min(j, n - j) + 1;
202+
}
203+
204+
for (let i = 1; i < m; ++i) {
205+
for (const j of pos[key.charCodeAt(i) - 'a'.charCodeAt(0)]) {
206+
for (const k of pos[key.charCodeAt(i - 1) - 'a'.charCodeAt(0)]) {
207+
f[i][j] = Math.min(
208+
f[i][j],
209+
f[i - 1][k] + Math.min(Math.abs(j - k), n - Math.abs(j - k)) + 1,
210+
);
211+
}
212+
}
213+
}
214+
215+
let ans: number = 1 << 30;
216+
for (const j of pos[key.charCodeAt(m - 1) - 'a'.charCodeAt(0)]) {
217+
ans = Math.min(ans, f[m - 1][j]);
218+
}
219+
return ans;
220+
}
221+
```
222+
177223
<!-- tabs:end -->
178224

179225
<!-- end -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function findRotateSteps(ring: string, key: string): number {
2+
const m: number = key.length;
3+
const n: number = ring.length;
4+
const pos: number[][] = Array.from({ length: 26 }, () => []);
5+
for (let i = 0; i < n; ++i) {
6+
const j: number = ring.charCodeAt(i) - 'a'.charCodeAt(0);
7+
pos[j].push(i);
8+
}
9+
10+
const f: number[][] = Array.from({ length: m }, () => Array(n).fill(1 << 30));
11+
for (const j of pos[key.charCodeAt(0) - 'a'.charCodeAt(0)]) {
12+
f[0][j] = Math.min(j, n - j) + 1;
13+
}
14+
15+
for (let i = 1; i < m; ++i) {
16+
for (const j of pos[key.charCodeAt(i) - 'a'.charCodeAt(0)]) {
17+
for (const k of pos[key.charCodeAt(i - 1) - 'a'.charCodeAt(0)]) {
18+
f[i][j] = Math.min(
19+
f[i][j],
20+
f[i - 1][k] + Math.min(Math.abs(j - k), n - Math.abs(j - k)) + 1,
21+
);
22+
}
23+
}
24+
}
25+
26+
let ans: number = 1 << 30;
27+
for (const j of pos[key.charCodeAt(m - 1) - 'a'.charCodeAt(0)]) {
28+
ans = Math.min(ans, f[m - 1][j]);
29+
}
30+
return ans;
31+
}

0 commit comments

Comments
 (0)