varminFallingPathSum=function(grid){constminFallingPathSumHelper=function(row,grid){if(row===grid.length){returnnewTriplet(0,0,0);}constnextRowTriplet=minFallingPathSumHelper(row+1,grid);letcurrentTriplet=newTriplet(Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,-1);for(letcol=0;col<grid[0].length;col++){constvalue=grid[row][col]+(col!==nextRowTriplet.minSumIndex?nextRowTriplet.minSum:nextRowTriplet.secondMinSum);if(value<=currentTriplet.minSum){currentTriplet.secondMinSum=currentTriplet.minSum;currentTriplet.minSum=value;currentTriplet.minSumIndex=col;}elseif(value<currentTriplet.secondMinSum){currentTriplet.secondMinSum=value;}}returncurrentTriplet;};constn=grid.length;returnminFallingPathSumHelper(0,grid).minSum;};classTriplet{constructor(minSum,secondMinSum,minSumIndex){this.minSum=minSum;this.secondMinSum=secondMinSum;this.minSumIndex=minSumIndex;}};
I do not fully understand this solution. It's a hard problem but not so hard. I really love this solution that I got from leetcode.
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse