Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

120. Triangle - JavaScript Solution With Explanation

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 :: generate package json for existing project 
Javascript :: creating hashblock method using sha256 hashing algorithm 
Javascript :: add attribute to element in jquery 
Javascript :: javascript find prime numbers 
Javascript :: how to chaage background color of any element from java script 
Javascript :: ms dyn crm associate n:m record js 
Javascript :: Self Invoking Function Tip 
Javascript :: import local js file node 
Javascript :: Javascript Area When All Sides are Known 
Javascript :: react console logs not working 
Javascript :: Object.entries() To Use For Of On JSON 
Javascript :: cubing timer 
Javascript :: add array and sort 
Javascript :: square brackets javascript object key 
Javascript :: prisma graphql n+1 problem solution 
Javascript :: animation in react stack overflow 
Javascript :: Validation Script Rule 
Javascript :: mogodb user get 
Javascript :: how to get on hnage input before clicking off 
Javascript :: How to redirect to login page if not logged in javascript 
Javascript :: selling partner api node install 
Javascript :: javascript odd or even 
Javascript :: parse json 
Javascript :: onhover 
Javascript :: append javascript variable to html 
Javascript :: dart to json 
Javascript :: js pow function 
Javascript :: pimcore 
Javascript :: vue__WEBPACK_IMPORTED_MODULE_0__.reactive) 
Javascript :: JavaScript Number Properties 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =