Skip to content

Commit 21f6c3c

Browse files
committed
2 parents 62d0ad5 + 6e0eee4 commit 21f6c3c

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

Algorithms/Dynamic Programming/longestCommonSequence.js

Whitespace-only changes.

Data-Structure/Array/2D/magicSum.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
function constructMagicSumMatrix(n) {
3+
let matrix = new Array(n);
4+
for (let index = 0; index < matrix.length; index++)
5+
matrix[index] = new Array(index);
6+
for (let i = 0; i < n; i++) {
7+
for (let j = 0; j < n; j++) {
8+
matrix[i][j] = -1;
9+
}
10+
}
11+
let i = 0,
12+
j = parseInt((n / 2)),
13+
num = 1;
14+
while (num <= (n * n)) {
15+
if (i == -1 && j == -1) {
16+
i = 1;
17+
j = 0;
18+
} else {
19+
if (i == -1) i = n - 1;
20+
if (j == -1) j = n - 1;
21+
}
22+
if (matrix[i][j] != -1) {
23+
i = i + 2;
24+
j++;
25+
continue;
26+
} else matrix[i][j] = num++;
27+
28+
i--;
29+
j--;
30+
}
31+
return matrix;
32+
}
33+
34+
function displayMatrix(matrix, n) {
35+
for (let i = 0; i < n; i++) {
36+
let row = '';
37+
for (let j = 0; j < n; j++) {
38+
row = row + matrix[i][j] + ' ';
39+
}
40+
console.log(row);
41+
}
42+
}
43+
let n = 7;
44+
let magicMatrix = constructMagicSumMatrix(n);
45+
displayMatrix(magicMatrix, n);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
function minimumMoves(visited, moves, n) {
4+
let result = {};
5+
result.vertex = 0;
6+
result.distance = 0;
7+
let queue = [result];
8+
let temp = null;
9+
while (queue.length != 0) {
10+
temp = queue.shift();
11+
if (result.vertex == n) break;
12+
for (let index = (temp.vertex + 1); index <= (temp.vertex + 6) && (temp.vertex < n); index++) {
13+
if (visited[index] == 0) {
14+
let qEntry = {};
15+
qEntry.distance = temp.distance + 1;
16+
visited[index] = 1;
17+
if (moves[index] != -1) {
18+
qEntry.vertex = moves[index];
19+
} else qEntry.vertex = index;
20+
queue.push(qEntry);
21+
}
22+
}
23+
}
24+
return temp.distance;
25+
}
26+
27+
let visited = [];
28+
let moves = [];
29+
let n = 100;
30+
for (let index = 0; index < n; index++) {
31+
visited[index] = 0;
32+
moves[index] = -1;
33+
}
34+
// Ladders Positions
35+
moves[2] = 23;
36+
moves[19] = 32;
37+
moves[66] = 82;
38+
moves[17] = 53;
39+
40+
// Snakes Positions
41+
moves[99] = 65;
42+
moves[36] = 18;
43+
moves[74] = 47;
44+
console.log('Minimum number of moves ', minimumMoves(visited, moves, n))

0 commit comments

Comments
 (0)