简体   繁体   中英

minimize of sum in javascript in two dimension array

I am trying to solve below problem ,my one test case fail why ?

Given amxn grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.


/**
 * @param {number[][]} grid
 * @return {number}
 */
var minPathSum = function(grid) {
      let firstRow = 0,
        firstColumn = 0,
        endColumn = 0,
        endRow = 0;
    if(grid.length === 1){
        return grid[0][0]
    }

    let firstArray = grid[0];
    let endArray = grid[grid.length - 1];
    firstArray.forEach((i) => firstRow += i);
    endArray.forEach((i) => endRow += i);
    for (let i = 1; i < grid.length - 1; i++) {
        firstColumn += grid[i].shift();
        endColumn += grid[i].pop()
    }
    let cornEdge = grid[grid.length - 1].pop();
    let firstElemt = grid[0].shift();
    if (firstRow + endColumn + cornEdge> firstElemt + firstColumn + endRow) {
        return firstElemt+ firstColumn + endRow
    } else {
        return firstRow + endColumn +cornEdge
    }
};

failed test case

Input
[[1,2,5],[3,2,1]]
Output
7
Expected
6

from my point of view above expectation is wrong ? it should be 7 using 1->3->2->1 but how it is six

You should do like:

 function min(array){ return Math.min(...array); } function sum(array){ return array.reduce((a, c)=>a+=c); } function sumsMin(arrayOfArrays){ const sums = []; arrayOfArrays.forEach(a=>{ sums.push(sum(a)); }); return min(sums); } console.log(sumsMin([[1,2,5],[3,2,1]]));

This is a brute force solution to the problem. It will recursively iterate every possible path through the array, and return the minimum found.

Unfortunately, whilst it works with the two test cases presented in the question, it exceeds the runtime execution limits on Leetcode for the more complex cases; although it does still yield an answer. For example:

minPathSum([
    [3,8,6,0,5,9,9,6,3,4,0,5,7,3,9,3],
    [0,9,2,5,5,4,9,1,4,6,9,5,6,7,3,2],
    [8,2,2,3,3,3,1,6,9,1,1,6,6,2,1,9],
    [1,3,6,9,9,5,0,3,4,9,1,0,9,6,2,7],
    [8,6,2,2,1,3,0,0,7,2,7,5,4,8,4,8],
    [4,1,9,5,8,9,9,2,0,2,5,1,8,7,0,9],
    [6,2,1,7,8,1,8,5,5,7,0,2,5,7,2,1],
    [8,1,7,6,2,8,1,2,2,6,4,0,5,4,1,3],
    [9,2,1,7,6,1,4,3,8,6,5,5,3,9,7,3],
    [0,6,0,2,4,3,7,6,1,3,8,6,9,0,0,8],
    [4,3,7,2,4,3,6,4,0,3,9,5,3,6,9,3],
    [2,1,8,8,4,5,6,5,8,7,3,7,7,5,8,3],
    [0,7,6,6,1,2,0,3,5,0,8,0,8,7,4,3],
    [0,4,3,4,9,0,1,9,7,7,8,6,4,6,9,5],
    [6,5,1,9,9,2,2,7,4,2,7,2,2,3,7,2],
    [7,1,9,6,1,2,7,0,9,6,6,4,4,5,1,0],
    [3,4,9,2,8,3,1,2,6,9,7,0,2,4,2,0],
    [5,1,8,8,4,6,8,5,2,4,1,6,2,2,9,7]
]);

// 83 = 3+0+8+2+2+3+3+3+1+0+0+0+2+0+2+5+0+2+0+5+4+1+3+3+8+3+3+3+5+2+0+0+7

 const minPathSum = function(grid) { let maxX = grid[0].length - 1; let maxY = grid.length - 1; return mapPath(grid, 0, 0, maxX, maxY, 0); }; const mapPath = function(grid, x, y, maxX, maxY, length) { const value = grid[y][x]; if (x === maxX && y === maxY) { return length + value; } const minX = (x < maxX) ? mapPath(grid, x + 1, y, maxX, maxY, length + value) : Infinity; const minY = (y < maxY) ? mapPath(grid, x, y + 1, maxX, maxY, length + value) : Infinity; return Math.min(minX, minY); }; console.log(minPathSum([[1,3,1],[1,5,1],[4,2,1]])); console.log(minPathSum([[1,2,5],[3,2,1]]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM