Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

120. Triangle - JavaScript Solution With Explantion

const minimumTotal = function(triangle) {
    const len = triangle.length
    const map = triangle[len - 1]
	/**
	* Example: [
	*     [2],
	*    [3,4],
	*   [6,5,7],
	*  [4,1,8,3]
	* ]
	* map = [4,1,8,3]
	*/
    for (let i = len - 2; i >= 0; i--) {
        for (let j = 0; j <= i; j++) {
            map[j] = Math.min(map[j], map[j + 1]) + triangle[i][j]
			/**
			*  first loop,  
			*  map[0] = Math.min(4 , 1) + 6 = 7
			*  map[1] = Math.min(1 , 8) + 5 = 6
			*  map[2] = Math.min(8 , 3) + 7 = 10
			*  
			*  second loop,  
			*  map[0] = Math.min(7 , 6) + 3 = 9
			*  map[1] = Math.min(6 , 10) + 4 = 10
			*  
			*  third loop,  
			*  map[0] = Math.min(9 , 10) + 2 = 11
			*/
        }
    } 
    return map[0]
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to install ghost js 
Javascript :: Creating getLastBlock Object for blockchain 
Javascript :: what is setImmediate vs clearImmediate 
Javascript :: Vue Js The specified value cannot be parsed, or is out of range 
Javascript :: ngrx angular Cannot add property 0, object is not extensible 
Javascript :: Javascript Encapsulation Inheritance Polymorphism Composition 
Javascript :: var a = x || y Variable Assignment In JavaScript 
Javascript :: change button text dynamically angular 6 
Javascript :: Javascript Area When Base and Height is Known 
Javascript :: React Native - iOS Release build crashing 
Javascript :: board in javascript 
Javascript :: Create A Class That Returns A Promise In Constructor 
Javascript :: Nodejs change host on npm run dev 
Javascript :: javascript hide div 
Javascript :: send a message in the first channel discord.js 
Javascript :: load limited data and search data from all in angularjs 
Javascript :: vue2-datepicker nuxtjs example 
Javascript :: Deployment of react static page using node and express 
Javascript :: Backbone With Express 
Javascript :: country select dropdown javascript 
Javascript :: Wrong Model Name For Backbone: Code Still Runs 
Javascript :: update excel file in react js using sheetjs 
Javascript :: Closure examples 
Javascript :: javascript array cheatsheet 
Javascript :: javascript alarm 
Javascript :: angular cli command to create component without spec 
Javascript :: animate js 
Javascript :: javascript prefill form 
Javascript :: p cannot appear as a descendant of p react 
Javascript :: JavaScript Number Objects 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =