97 lines
		
	
	
		
			27 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			97 lines
		
	
	
		
			27 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | ||
|  | id: 5900f3b01000cf542c50fec2 | ||
|  | title: 'Problem 67: Maximum path sum II' | ||
|  | challengeType: 5 | ||
|  | forumTopicId: 302179 | ||
|  | dashedName: problem-67-maximum-path-sum-ii | ||
|  | --- | ||
|  | 
 | ||
|  | # --description--
 | ||
|  | 
 | ||
|  | By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. | ||
|  | 
 | ||
|  | <div style='text-align: center;'> | ||
|  |   <strong style='color: red;'>3</strong><br> | ||
|  |   <strong style='color: red;'>7</strong> 4<br> | ||
|  |   2 <strong style='color: red;'>4</strong> 6<br> | ||
|  |   8 5 <strong style='color: red;'>9</strong> 3 | ||
|  | </div> | ||
|  | 
 | ||
|  | That is, 3 + 7 + 4 + 9 = 23. | ||
|  | 
 | ||
|  | Find the maximum total from top to bottom in `numTriangle`, a 2D array defined in the background containing a triangle with one-hundred rows. | ||
|  | 
 | ||
|  | **Note:** This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 2<sup>99</sup> altogether! If you could check one trillion (10<sup>12</sup>) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o) | ||
|  | 
 | ||
|  | # --hints--
 | ||
|  | 
 | ||
|  | `maximumPathSumII(testTriangle)` should return a number. | ||
|  | 
 | ||
|  | ```js | ||
|  | assert(typeof maximumPathSumII(_testTriangle) === 'number'); | ||
|  | ``` | ||
|  | 
 | ||
|  | `maximumPathSumII(testTriangle)` should return 23. | ||
|  | 
 | ||
|  | ```js | ||
|  | assert.strictEqual(maximumPathSumII(_testTriangle), 23); | ||
|  | ``` | ||
|  | 
 | ||
|  | `maximumPathSumII(numTriangle)` should return 7273. | ||
|  | 
 | ||
|  | ```js | ||
|  | assert.strictEqual(maximumPathSumII(_numTriangle), 7273); | ||
|  | ``` | ||
|  | 
 | ||
|  | # --seed--
 | ||
|  | 
 | ||
|  | ## --before-user-code--
 | ||
|  | 
 | ||
|  | ```js | ||
|  | const _testTriangle = [[3, 0, 0, 0], | ||
|  |                       [7, 4, 0, 0], | ||
|  |                       [2, 4, 6, 0], | ||
|  |                       [8, 5, 9, 3]]; | ||
|  | const _numTriangle = [[59,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[73,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[52,40,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[26,53,6,34,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[10,51,87,86,81,0,0,0,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## --seed-contents--
 | ||
|  | 
 | ||
|  | ```js | ||
|  | function maximumPathSumII(triangle) { | ||
|  | 
 | ||
|  |   return true; | ||
|  | } | ||
|  | 
 | ||
|  | const testTriangle = [[3, 0, 0, 0], | ||
|  |                       [7, 4, 0, 0], | ||
|  |                       [2, 4, 6, 0], | ||
|  |                       [8, 5, 9, 3]]; | ||
|  | 
 | ||
|  | maximumPathSumII(testTriangle); | ||
|  | ``` | ||
|  | 
 | ||
|  | # --solutions--
 | ||
|  | 
 | ||
|  | ```js | ||
|  | function maximumPathSumII(triangle) { | ||
|  |   const newTriangle = []; | ||
|  |   for (let i = 0; i < triangle.length; i++) { | ||
|  |     newTriangle.push(triangle[i].slice()); | ||
|  |   } | ||
|  | 
 | ||
|  |   for (let i = newTriangle.length - 2; i >= 0; i--) { | ||
|  |     for (let j = i; j >= 0; j--) { | ||
|  |       let higherOption = 0; | ||
|  |       if (newTriangle[i + 1][j + 1] > newTriangle[i + 1][j]) { | ||
|  |         higherOption = newTriangle[i + 1][j + 1]; | ||
|  |       } else { | ||
|  |         higherOption = newTriangle[i + 1][j]; | ||
|  |       } | ||
|  |       newTriangle[i][j] += higherOption; | ||
|  |     } | ||
|  |   } | ||
|  |   return newTriangle[0][0]; | ||
|  | } | ||
|  | ``` |