简体   繁体   English

最小化二维数组中javascript中的总和

[英]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.给定填充了非负数的 amxn 网格,找到一条从左上角到右下角的路径,该路径最小化沿其路径的所有数字的总和。

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使用1->3->2->1应该是7但它是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;不幸的是,虽然它适用于问题中提出的两个测试用例,但对于更复杂的用例,它超出了Leetcode的运行时执行限制; 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]]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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