Skip to content

Commit dcc4a90

Browse files
committed
Added medium/longest_matrix_path
1 parent 6baacc1 commit dcc4a90

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

medium/longest_matrix_path.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Using the JavaScript language, have the function longestMatrixPath(strArr)
3+
* take the array of strings stored in strArr, which will be an NxM matrix of
4+
* positive single-digit integers, and find the longest increasing path composed
5+
* of distinct integers. When moving through the matrix, you can only go up,
6+
* down, left, and right. For example: if strArr is ["345", "326", "221"], then
7+
* this looks like the following matrix:
8+
*
9+
* 3 4 5
10+
* 3 2 6
11+
* 2 2 1
12+
*
13+
* For the input above, the longest increasing path goes from: 3 -> 4 -> 5 -> 6.
14+
* Your program should return the number of connections in the longest path, so
15+
* therefore for this input your program should return 3. There may not
16+
* necessarily always be a longest path within the matrix.
17+
*
18+
* https://www.coderbyte.com/results/bhanson:Longest%20Matrix%20Path:JavaScript
19+
*
20+
* @param {array} strArr
21+
* @return {number}
22+
*/
23+
function longestMatrixPath(strArr) {
24+
let longestPath = 0;
25+
26+
for (let row = 0; row < strArr.length; row++) {
27+
for (let col = 0; col < strArr[0].length; col++) {
28+
let longestSubPath = tryDirections(strArr, col, row);
29+
if (longestSubPath.length > longestPath) {
30+
longestPath = longestSubPath.length;
31+
}
32+
}
33+
}
34+
35+
return longestPath - 1;
36+
}
37+
38+
function tryDirections(matrix, x, y, visited = []) {
39+
// Arrays are passed by reference so we'll create a copy
40+
visited = visited.slice();
41+
42+
markVisited(visited, x, y);
43+
44+
const directions = [
45+
{ x: 1, y: 0 }, // right
46+
{ x: 0, y: 1 }, // down
47+
{ x: -1, y: 0 }, // left
48+
{ x: 0, y: -1 } // up
49+
];
50+
51+
const children = [];
52+
53+
directions.forEach(direction => {
54+
const [tryX, tryY] = [x + direction.x, y + direction.y];
55+
56+
if (
57+
tryX < 0 ||
58+
tryX >= matrix[0].length ||
59+
tryY < 0 ||
60+
tryY >= matrix.length ||
61+
hasVisited(visited, tryX, tryY) ||
62+
Number(matrix[tryY][tryX]) <= Number(matrix[y][x])
63+
) {
64+
// Invalid coords
65+
return [];
66+
}
67+
68+
children.push(tryDirections(matrix, tryX, tryY, visited));
69+
});
70+
71+
children.sort((a, b) => b.length - a.length);
72+
73+
if (children.length > 0) {
74+
return children[0];
75+
}
76+
77+
return visited;
78+
79+
function markVisited(visited, x, y) {
80+
visited.push({ x, y });
81+
}
82+
83+
function hasVisited(visited, x, y) {
84+
return visited.some(coords => coords.x === x && coords.y === y);
85+
}
86+
}
87+
88+
module.exports = longestMatrixPath;

medium/longest_matrix_path.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const longestMatrixPath = require('./longest_matrix_path');
2+
3+
describe('longestMatrixPath()', () => {
4+
test('correctly returns number of connections to longest path', () => {
5+
expect(longestMatrixPath(['345', '326', '221'])).toBe(3);
6+
expect(longestMatrixPath(['12256', '56219', '43215'])).toBe(5);
7+
expect(longestMatrixPath(['67', '21', '45'])).toBe(3);
8+
});
9+
10+
test('passes Coderbyte.com tests', () => {
11+
expect(longestMatrixPath(['345', '326', '221'])).toBe(3);
12+
13+
expect(longestMatrixPath(['12256', '56219', '43215'])).toBe(5);
14+
15+
expect(longestMatrixPath(['67', '21', '45'])).toBe(3);
16+
17+
expect(longestMatrixPath(['111', '111', '111'])).toBe(0);
18+
19+
expect(
20+
longestMatrixPath(['564215', '287532', '193167', '111123'])
21+
).toBe(5);
22+
23+
expect(longestMatrixPath(['123', '456', '789'])).toBe(4);
24+
25+
expect(longestMatrixPath(['923', '456', '789'])).toBe(3);
26+
27+
expect(longestMatrixPath(['92', '15', '98', '32'])).toBe(3);
28+
29+
expect(longestMatrixPath(['9', '1', '9', '3', '1'])).toBe(2);
30+
31+
expect(longestMatrixPath(['9452124643115673264'])).toBe(3);
32+
});
33+
});

0 commit comments

Comments
 (0)