Skip to content

Commit eb890fe

Browse files
committed
⚡ improve: prefer Unit32Array instead of number[]
1 parent 0c3ab92 commit eb890fe

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

Diff for: packages/manacher/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ If you are interested in this algorithm, you can check [here][manacher].
8585
```typescript
8686
import manacher from '@algorithm.ts/manacher'
8787

88-
export function minCut(s: string): number {
89-
const N: number = s.length
90-
const f: number[] = manacher(s)
88+
export function minCut(text: string): number {
89+
const N: number = text.length
90+
const radius: Uint32Array= manacher(text)
9191
const dp: Uint32Array = new Uint32Array(N)
9292

9393
for (let i = 1; i < N; ++i) {
94-
let answer: number = i < f[i] * 2 ? 0 : dp[i - 1] + 1
94+
let answer: number = i < radius[i] * 2 ? 0 : dp[i - 1] + 1
9595
if (answer > 0) {
9696
for (let k = 1; k < i; ++k) {
97-
if (i - k < f[i + k] * 2) {
97+
if (i - k < radius[i + k] * 2) {
9898
answer = Math.min(answer, dp[k - 1] + 1)
9999
}
100100
}

Diff for: packages/manacher/__test__/manacher.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ test('manacher', function () {
3939
*/
4040
function solution1(text: string): number {
4141
const N: number = text.length
42-
const f: number[] = manacher(text)
42+
const radius: Uint32Array = manacher(text)
4343
const dp: Uint32Array = new Uint32Array(N)
4444

4545
for (let i = 1; i < N; ++i) {
46-
let answer: number = i < f[i] * 2 ? 0 : dp[i - 1] + 1
46+
let answer: number = i < radius[i] * 2 ? 0 : dp[i - 1] + 1
4747
if (answer > 0) {
4848
for (let k = 1; k < i; ++k) {
49-
if (i - k < f[i + k] * 2) {
49+
if (i - k < radius[i + k] * 2) {
5050
answer = Math.min(answer, dp[k - 1] + 1)
5151
}
5252
}

Diff for: packages/manacher/src/index.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* Find the longest palindrome length centered at each position in the given
33
* text string within the complexity of $O(N)$.
44
*
5-
* Note: len is an array with a length of $2N-1$, which represents the radius
5+
* Note: radius is an array with a length of $2N-1$, which represents the radius
66
* of the palindrome:
77
*
8-
* - len[2i] is equal to the radius of the longest palindrome centered at
8+
* - radius[2i] is equal to the radius of the longest palindrome centered at
99
* the position (i, i)
1010
*
11-
* - len[2i+1] is equal to the radius of the longest palindrome centered at
11+
* - radius[2i+1] is equal to the radius of the longest palindrome centered at
1212
* the position (i, i+1)
1313
*
1414
* ## Examples
@@ -17,34 +17,34 @@
1717
*
1818
* ==>
1919
*
20-
* len[3] = 2 // abba
21-
* len[6] = 2 // bab
20+
* radius[3] = 2 // abba
21+
* radius[6] = 2 // bab
2222
*
2323
* @param text
24-
* @param len
24+
* @param radius
2525
* @param N
2626
*
2727
* @see https://me.guanghechen.com/post/algorithm/string/manacher/
2828
*/
29-
export function manacher(text: string): number[] {
29+
export function manacher(text: string): Uint32Array {
3030
const N = text.length
31-
const _size = N * 2 - 1
32-
const len: number[] = new Array(_size)
31+
const _size = (N << 1) - 1
32+
const radius: Uint32Array = new Uint32Array(_size)
3333

34-
len[0] = 1
34+
radius[0] = 1
3535
for (let i = 1, j = 0; i < _size; ++i) {
3636
const p: number = i >> 1
3737
const q: number = i - p
38-
const r: number = ((j + 1) >> 1) + len[j] - 1
38+
const r: number = ((j + 1) >> 1) + radius[j] - 1
3939

40-
let L = r < q ? 0 : Math.min(r - q + 1, len[(j << 1) - i])
40+
let L = r < q ? 0 : Math.min(r - q + 1, radius[(j << 1) - i])
4141
while (p > L - 1 && q + L < N && text[p - L] === text[q + L]) L += 1
42-
len[i] = L
42+
radius[i] = L
4343

4444
// Update j
4545
if (q + L - 1 > r) j = i
4646
}
47-
return len
47+
return radius
4848
}
4949

5050
export default manacher

0 commit comments

Comments
 (0)