zoukankan      html  css  js  c++  java
  • 120. Triangle(js)

    120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

    For example, given the following triangle

    [
         [2],
        [3,4],
       [6,5,7],
      [4,1,8,3]
    ]
    

    The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

    题意:给定一个三角形的二维数组,求数组顶部到底部的权值最小的连续路径

    代码如下:

    /**
     * @param {number[][]} triangle
     * @return {number}
     */
    //每走一步要保存上一步的索引
    var minimumTotal = function(triangle) {
        
        if(triangle.length===0) return 0;
        
       var res=[];
        for(var i=0;i<=triangle.length;i++){
            res.push(0);
        }
      
        for(var i=triangle.length-1;i>=0;i--){
           for(var j=0;j<triangle[i].length;j++){
               res[j]=Math.min(res[j+1],res[j])+triangle[i][j];
           }
        }
        return res[0];
      
    };
  • 相关阅读:
    6-1面向对象
    5-1模块
    python随机数
    4-5目录
    4-4内置函数
    4-3迭代器和生成器
    4-1装饰器1
    4-2装饰器2
    3-4函数-全局变量
    3-5递归-函数
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/10828532.html
Copyright © 2011-2022 走看看