|
| 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; |
0 commit comments