Skip to content

Commit

Permalink
refactor(JS, TS): Improve code readability in edit_distance (krahets#860
Browse files Browse the repository at this point in the history
)

* fix(JS、TS): chapter_sorting QuickSortMedian and QuickSortTailCall example

* refactor(JS、TS): Improve code readability in edit_distance

* Updated code
  • Loading branch information
reeswell authored Oct 24, 2023
1 parent d27890d commit 88a1e4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
12 changes: 4 additions & 8 deletions codes/javascript/chapter_dynamic_programming/edit_distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function editDistanceDFS(s, t, i, j) {
const del = editDistanceDFS(s, t, i - 1, j);
const replace = editDistanceDFS(s, t, i - 1, j - 1);
// 返回最少编辑步数
return Math.min(Math.min(insert, del), replace) + 1;
return Math.min(insert, del, replace) + 1;
}

/* 编辑距离:记忆化搜索 */
Expand All @@ -50,7 +50,7 @@ function editDistanceDFSMem(s, t, mem, i, j) {
const del = editDistanceDFSMem(s, t, mem, i - 1, j);
const replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// 记录并返回最少编辑步数
mem[i][j] = Math.min(Math.min(insert, del), replace) + 1;
mem[i][j] = Math.min(insert, del, replace) + 1;
return mem[i][j];
}

Expand All @@ -74,11 +74,7 @@ function editDistanceDP(s, t) {
dp[i][j] = dp[i - 1][j - 1];
} else {
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
dp[i][j] =
Math.min(
Math.min(dp[i][j - 1], dp[i - 1][j]),
dp[i - 1][j - 1]
) + 1;
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
}
}
}
Expand Down Expand Up @@ -107,7 +103,7 @@ function editDistanceDPComp(s, t) {
dp[j] = leftup;
} else {
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;
dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;
}
leftup = temp; // 更新为下一轮的 dp[i-1, j-1]
}
Expand Down
12 changes: 4 additions & 8 deletions codes/typescript/chapter_dynamic_programming/edit_distance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function editDistanceDFS(s: string, t: string, i: number, j: number): number {
const del = editDistanceDFS(s, t, i - 1, j);
const replace = editDistanceDFS(s, t, i - 1, j - 1);
// 返回最少编辑步数
return Math.min(Math.min(insert, del), replace) + 1;
return Math.min(insert, del, replace) + 1;
}

/* 编辑距离:记忆化搜索 */
Expand Down Expand Up @@ -56,7 +56,7 @@ function editDistanceDFSMem(
const del = editDistanceDFSMem(s, t, mem, i - 1, j);
const replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// 记录并返回最少编辑步数
mem[i][j] = Math.min(Math.min(insert, del), replace) + 1;
mem[i][j] = Math.min(insert, del, replace) + 1;
return mem[i][j];
}

Expand All @@ -82,11 +82,7 @@ function editDistanceDP(s: string, t: string): number {
dp[i][j] = dp[i - 1][j - 1];
} else {
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
dp[i][j] =
Math.min(
Math.min(dp[i][j - 1], dp[i - 1][j]),
dp[i - 1][j - 1]
) + 1;
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
}
}
}
Expand Down Expand Up @@ -115,7 +111,7 @@ function editDistanceDPComp(s: string, t: string): number {
dp[j] = leftup;
} else {
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;
dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;
}
leftup = temp; // 更新为下一轮的 dp[i-1, j-1]
}
Expand Down

0 comments on commit 88a1e4a

Please sign in to comment.