|
| 1 | +/** |
| 2 | + * @file Provides solution for Project Euler Problem 18 - Maximum path sum I |
| 3 | + * @author Eric Lavault {@link https://github.com/lvlte} |
| 4 | + * @license MIT |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Problem 18 - Maximum path sum I |
| 9 | + * |
| 10 | + * @see {@link https://projecteuler.net/problem=18} |
| 11 | + * |
| 12 | + * By starting at the top of the triangle below and moving to adjacent numbers |
| 13 | + * on the row below, the maximum total from top to bottom is 23 : |
| 14 | + * |
| 15 | + * 3 |
| 16 | + * 7 4 |
| 17 | + * 2 4 6 |
| 18 | + * 8 5 9 3 |
| 19 | + * |
| 20 | + * That is, 3 +たす 7 +たす 4 +たす 9 =わ 23. |
| 21 | + * |
| 22 | + * Find the maximum total from top to bottom of the triangle below : |
| 23 | + * |
| 24 | + * 75 |
| 25 | + * 95 64 |
| 26 | + * 17 47 82 |
| 27 | + * 18 35 87 10 |
| 28 | + * 20 04 82 47 65 |
| 29 | + * 19 01 23 75 03 34 |
| 30 | + * 88 02 77 73 07 63 67 |
| 31 | + * 99 65 04 28 06 16 70 92 |
| 32 | + * 41 41 26 56 83 40 80 70 33 |
| 33 | + * 41 48 72 33 47 32 37 16 94 29 |
| 34 | + * 53 71 44 65 25 43 91 52 97 51 14 |
| 35 | + * 70 11 33 28 77 73 17 78 39 68 17 57 |
| 36 | + * 91 71 52 38 17 14 91 43 58 50 27 29 48 |
| 37 | + * 63 66 04 68 89 53 67 30 73 16 69 87 40 31 |
| 38 | + * 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 |
| 39 | + * |
| 40 | + * NOTE: As there are only 16384 routes, it is possible to solve this problem |
| 41 | + * by trying every route. However, Problem 67, is the same challenge with a |
| 42 | + * triangle containing one-hundred rows; it cannot be solved by brute force, |
| 43 | + * and requires a clever method! ;o) |
| 44 | + */ |
| 45 | + |
| 46 | +const triangle = ` |
| 47 | +75 |
| 48 | +95 64 |
| 49 | +17 47 82 |
| 50 | +18 35 87 10 |
| 51 | +20 04 82 47 65 |
| 52 | +19 01 23 75 03 34 |
| 53 | +88 02 77 73 07 63 67 |
| 54 | +99 65 04 28 06 16 70 92 |
| 55 | +41 41 26 56 83 40 80 70 33 |
| 56 | +41 48 72 33 47 32 37 16 94 29 |
| 57 | +53 71 44 65 25 43 91 52 97 51 14 |
| 58 | +70 11 33 28 77 73 17 78 39 68 17 57 |
| 59 | +91 71 52 38 17 14 91 43 58 50 27 29 48 |
| 60 | +63 66 04 68 89 53 67 30 73 16 69 87 40 31 |
| 61 | +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 |
| 62 | +` |
| 63 | + |
| 64 | +export const maxPathSum = function (grid = triangle) { |
| 65 | + /** |
| 66 | + * If we reduce the problem to its simplest form, considering : |
| 67 | + * |
| 68 | + * 7 -> The max sum depends on the two adjacent numbers below 7, |
| 69 | + * 2 4 not 7 itself. |
| 70 | + * |
| 71 | + * obviously 4 > 2 therefore the max sum is 7 + 4 = 11 |
| 72 | + * |
| 73 | + * 6 |
| 74 | + * Likewise, with : 4 6 6 > 4 therefore the max sum is 6 + 6 = 12 |
| 75 | + * |
| 76 | + * Now, let's say we are given : |
| 77 | + * |
| 78 | + * 3 |
| 79 | + * 7 6 |
| 80 | + * 2 4 6 |
| 81 | + * |
| 82 | + * and we decompose it into sub-problems such that each one fits the simple |
| 83 | + * case above, we got : |
| 84 | + * |
| 85 | + * . . 3 |
| 86 | + * 7 . . 6 ? ? |
| 87 | + * 2 4 . . 4 6 . . . |
| 88 | + * |
| 89 | + * Again, considering any number, the best path depends on the two adjacent |
| 90 | + * numbers below it, not the number itself. That's why we have to compute |
| 91 | + * the max sum from bottom to top, replacing each number with the sum of |
| 92 | + * that number plus the greatest of the two adjacent numbers computed from |
| 93 | + * the previous row. |
| 94 | + * |
| 95 | + * . . 3 15 |
| 96 | + * 11 . . 12 -> 11 12 -> x x |
| 97 | + * x x . . x x x x x x x x |
| 98 | + * |
| 99 | + * We are simplifying a complicated problem by breaking it down into simpler |
| 100 | + * sub-problems in a recursive manner, this is called Dynamic Programming. |
| 101 | + */ |
| 102 | + |
| 103 | + grid = grid.split(/\r\n|\n/).filter(l => l).map(r => r.split(' ').map(n => +n)) |
| 104 | + |
| 105 | + for (let i = grid.length - 2; i >= 0; i--) { |
| 106 | + for (let j = 0; j < grid[i].length; j++) { |
| 107 | + grid[i][j] += Math.max(grid[i + 1][j], grid[i + 1][j + 1]) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return grid[0][0] |
| 112 | +} |
0 commit comments