Skip to content

Commit a5a2599

Browse files
committed
adding Largest Common Subsequence and eslint
1 parent 1c66158 commit a5a2599

File tree

6 files changed

+748
-0
lines changed

6 files changed

+748
-0
lines changed

.eslintrc.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
env: {
3+
es6: true,
4+
jest: true,
5+
node: true
6+
},
7+
rules: {
8+
"no-undef": "error",
9+
"no-unused-vars": "warn",
10+
"no-console": "warn"
11+
},
12+
extends: ["eslint:recommended"]
13+
};

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.tabSize": 2
3+
}

LargestCommonSubarray.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function LargestCommonSubarray(a, b) {
2+
const m = a.length,
3+
n = b.length;
4+
5+
let max = [];
6+
7+
// make a 2d array d of size m,n with all blank arrays
8+
// double loop(i,j) on a and b
9+
// d[i][j] = d[i-1][j-1].concat(a[i]) if a[i] == b[i]
10+
11+
const d = Array(m)
12+
.fill()
13+
.map(() =>
14+
Array(n)
15+
.fill()
16+
.map(() => [])
17+
);
18+
19+
for (let i = 0; i < m; i++) {
20+
for (let j = 0; j < n; j++) {
21+
if (a[i] == b[j]) {
22+
if (i > 0 && j > 0) {
23+
d[i][j] = d[i - 1][j - 1].concat(a[i]);
24+
} else {
25+
d[i][j] = [a[i]];
26+
}
27+
}
28+
max = d[i][j].length > max.length ? d[i][j] : max;
29+
}
30+
}
31+
// console.log(d);
32+
return max;
33+
}
34+
35+
function lengthLargestCommonSubarray(A, B) {
36+
var m = A.length,
37+
n = B.length;
38+
var d = new Array(m).fill().map(() => new Array(n).fill(0));
39+
d[0][0] = A[0] == B[0] ? 1 : 0;
40+
d[0][1] = 0;
41+
d[1][0] = 0;
42+
43+
let max = Number.MIN_SAFE_INTEGER;
44+
45+
for (var i = 0; i < m; i++) {
46+
for (var j = 0; j < n; j++) {
47+
if (A[i] == B[j]) {
48+
if (i <= 0 || j <= 0) {
49+
d[i][j] = 1;
50+
} else {
51+
d[i][j] = d[i - 1][j - 1] + 1;
52+
}
53+
}
54+
max = d[i][j] > max ? d[i][j] : max;
55+
}
56+
}
57+
return max;
58+
}
59+
60+
const A = [1, 2, 3, 2, 1],
61+
B = [3, 2, 1, 4, 7];
62+
63+
// eslint-disable-next-line
64+
console.log(LargestCommonSubarray(A, B), lengthLargestCommonSubarray(A, B));

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "practice",
3+
"version": "1.0.0",
4+
"description": "Problem solving with js",
5+
"main": "index.js",
6+
"repository": "git@github.com:smishr4/practice.git",
7+
"author": "shubham <shubhammshr621@gmail.com>",
8+
"license": "MIT",
9+
"dependencies": {},
10+
"devDependencies": {
11+
"eslint": "^5.16.0"
12+
}
13+
}

0 commit comments

Comments
 (0)