mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
longestCommonSubsequence optimization
The lcsMatrix creation can be optimized.
This commit is contained in:
parent
94abfec91d
commit
651d95c437
@ -5,17 +5,7 @@
|
||||
*/
|
||||
export default function longestCommonSubsequence(set1, set2) {
|
||||
// Init LCS matrix.
|
||||
const lcsMatrix = Array(set2.length + 1).fill(null).map(() => Array(set1.length + 1).fill(null));
|
||||
|
||||
// Fill first row with zeros.
|
||||
for (let columnIndex = 0; columnIndex <= set1.length; columnIndex += 1) {
|
||||
lcsMatrix[0][columnIndex] = 0;
|
||||
}
|
||||
|
||||
// Fill first column with zeros.
|
||||
for (let rowIndex = 0; rowIndex <= set2.length; rowIndex += 1) {
|
||||
lcsMatrix[rowIndex][0] = 0;
|
||||
}
|
||||
const lcsMatrix = Array(set2.length + 1).fill().map(() => Array(set1.length + 1).fill().map(() => 0));
|
||||
|
||||
// Fill rest of the column that correspond to each of two strings.
|
||||
for (let rowIndex = 1; rowIndex <= set2.length; rowIndex += 1) {
|
||||
|
Loading…
Reference in New Issue
Block a user