diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-83-path-sum-four-ways.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-83-path-sum-four-ways.md index 40a008132a1..bd3b6f085a1 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-83-path-sum-four-ways.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-83-path-sum-four-ways.md @@ -10,46 +10,51 @@ dashedName: problem-83-path-sum-four-ways **Note:** This problem is a significantly more challenging version of Problem 81. -In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red and is equal to 2297. +In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red and is equal to `2297`. -
- $\begin{pmatrix} + $$\begin{pmatrix} \color{red}{131} & 673 & \color{red}{234} & \color{red}{103} & \color{red}{18}\\\\ \color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & \color{red}{150}\\\\ 630 & 803 & 746 & \color{red}{422} & \color{red}{111}\\\\ 537 & 699 & 497 & \color{red}{121} & 956\\\\ 805 & 732 & 524 & \color{red}{37} & \color{red}{331} - \end{pmatrix}$ -
+ \end{pmatrix}$$ -Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down in `matrix`, a 2D array defined in the background, containing an 80 by 80 matrix. +Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down in `matrix`, a 2D array representing a matrix. The maximum matrix size used in tests will be 80 by 80. # --hints-- -`pathSumFourWays(testMatrix)` should return a number. +`pathSumFourWays(testMatrix1)` should return a number. ```js -assert(typeof pathSumFourWays(testMatrix) === 'number'); +assert(typeof pathSumFourWays(_testMatrix1) === 'number'); ``` -`pathSumFourWays(testMatrix)` should return 2297. +`pathSumFourWays(testMatrix1)` should return `2297`. ```js -assert.strictEqual(pathSumFourWays(testMatrix), 2297); +assert.strictEqual(pathSumFourWays(_testMatrix1), 2297); ``` -`pathSumFourWays(matrix)` should return 425185. +`pathSumFourWays(testMatrix2)` should return `425185`. ```js -assert.strictEqual(pathSumFourWays(matrix), 425185); +assert.strictEqual(pathSumFourWays(_testMatrix2), 425185); ``` # --seed-- -## --before-user-code-- +## --after-user-code-- ```js -const matrix = [ +const _testMatrix1 = [ + [131, 673, 234, 103, 18], + [201, 96, 342, 965, 150], + [630, 803, 746, 422, 111], + [537, 699, 497, 121, 956], + [805, 732, 524, 37, 331] +]; +const _testMatrix2 = [ [4445,2697,5115,718,2209,2212,654,4348,3079,6821,7668,3276,8874,4190,3785,2752,9473,7817,9137,496,7338,3434,7152,4355,4552,7917,7827,2460,2350,691,3514,5880,3145,7633,7199,3783,5066,7487,3285,1084,8985,760,872,8609,8051,1134,9536,5750,9716,9371,7619,5617,275,9721,2997,2698,1887,8825,6372,3014,2113,7122,7050,6775,5948,2758,1219,3539,348,7989,2735,9862,1263,8089,6401,9462,3168,2758,3748,5870], [1096,20,1318,7586,5167,2642,1443,5741,7621,7030,5526,4244,2348,4641,9827,2448,6918,5883,3737,300,7116,6531,567,5997,3971,6623,820,6148,3287,1874,7981,8424,7672,7575,6797,6717,1078,5008,4051,8795,5820,346,1851,6463,2117,6058,3407,8211,117,4822,1317,4377,4434,5925,8341,4800,1175,4173,690,8978,7470,1295,3799,8724,3509,9849,618,3320,7068,9633,2384,7175,544,6583,1908,9983,481,4187,9353,9377], [9607,7385,521,6084,1364,8983,7623,1585,6935,8551,2574,8267,4781,3834,2764,2084,2669,4656,9343,7709,2203,9328,8004,6192,5856,3555,2260,5118,6504,1839,9227,1259,9451,1388,7909,5733,6968,8519,9973,1663,5315,7571,3035,4325,4283,2304,6438,3815,9213,9806,9536,196,5542,6907,2475,1159,5820,9075,9470,2179,9248,1828,4592,9167,3713,4640,47,3637,309,7344,6955,346,378,9044,8635,7466,5036,9515,6385,9230], @@ -136,14 +141,14 @@ const matrix = [ ## --seed-contents-- ```js -function pathSumFourWays(arr) { +function pathSumFourWays(matrix) { return true; } // Only change code above this line -const testMatrix = [ +const testMatrix1 = [ [131, 673, 234, 103, 18], [201, 96, 342, 965, 150], [630, 803, 746, 422, 111], @@ -151,11 +156,64 @@ const testMatrix = [ [805, 732, 524, 37, 331] ]; -pathSumFourWays(testMatrix); +pathSumFourWays(testMatrix1); ``` # --solutions-- ```js -// solution required +function pathSumFourWays(matrix) { + const MOVES = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1] + ]; + + function getAllowedMoves(row, col) { + const moves = []; + for (let i = 0; i < MOVES.length; i++) { + const [rowChange, colChange] = MOVES[i]; + const nextRow = row + rowChange; + const nextCol = col + colChange; + if (isMoveAllowed(nextRow, nextCol)) { + moves.push([nextRow, nextCol]); + } + } + return moves; + } + + function isMoveAllowed(nextRow, nextCol) { + if (nextRow < 0 || nextRow >= size || nextCol < 0 || nextCol >= size) { + return false; + } + return true; + } + + const size = matrix.length; + const resultMatrix = new Array(size) + .fill() + .map(() => new Array(size).fill(Infinity)); + + const [startRow, startCol] = new Array(2).fill(size - 1); + const queue = [[startRow, startCol]]; + resultMatrix[startRow][startCol] = matrix[startRow][startCol]; + + while (queue.length > 0) { + const [curRow, curCol] = queue.shift(); + + const allowedMoves = getAllowedMoves(curRow, curCol); + + for (let i = 0; i < allowedMoves.length; i++) { + const [nextRow, nextCol] = allowedMoves[i]; + const bestPath = resultMatrix[nextRow][nextCol]; + const newPath = resultMatrix[curRow][curCol] + matrix[nextRow][nextCol]; + if (newPath < bestPath) { + resultMatrix[nextRow][nextCol] = newPath; + queue.push(allowedMoves[i]); + } + } + } + return resultMatrix[0][0]; +} ```