zoukankan      html  css  js  c++  java
  • 【Leetcode】【Medium】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).

    Note:
    Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

    解题思路1:

    使用动态规划的思路求解(解法2更符合动态规划思想)。

    题目要求只能使用n的额外空间,首先想到了不断迭代数组的方法。

    新建包含n个元素的数组A,初始值A[0] = triangle[0][0],即triangle第一层那唯一一个元素的值。

    A表示路径运行到当前位置的最小值,之后从第二层到最后一层不断迭代,不断计算到达新一层各个位置的最小值,迭代结束后,返回数组A中记录的最小路径值,即为题目所求。

    需要注意的是:

    1,因为后一层比前一层多一个元素,所以每层首尾元素在计算时需要特殊对待:

      A[0] = triangle[i][0] + A[0];

      A[i-1] =  triangle[i][i-1] + A[i]

     其他元素公式为:

      A[j] = triangle[i][j] + min(A[j], A[j-1])

    2、迭代数组过程,由于新旧值交替运算,很容易出错的地方是,计算一个新值时覆盖了仍需使用的旧值,导致计算其他新值时出错;

     针对本题:

      除了首尾元素外,每次计算一个新值A[j],都需要用到旧的数组中保存的A[j]和A[j-1]的值;

      因此从后往前更新,可以避免出现错误计算。

    代码:

     1 class Solution {
     2 public:
     3     int minimumTotal(vector<vector<int> > &triangle) {
     4         int n = triangle.size();
     5         vector<int> path(n);
     6         path[0] = triangle[0][0];
     7         
     8         for (int i = 1; i < n; ++i) {
     9             path[i] = triangle[i][i] + path[i-1];
    10             for (int j = i - 1; j > 0; --j) {
    11                 path[j] = triangle[i][j] + min(path[j], path[j-1]);
    12             }
    13             path[0] = triangle[i][0] + path[0];
    14         }
    15         
    16         int min = path[0];
    17         for (int i = 1; i < n; ++i){
    18             if (path[i] < min)
    19                 min = path[i];
    20         }
    21         
    22         return min;
    23     }
    24 };

    解题思路2:

    上一解中存在两个问题:

    1、由于从上到下计算,每层比上一层元素多,造成必须单独计算首尾元素,代码丑陋,可读性降低;

      解决:如果从下向上迭代运算,则可以解决这个问题;

    2、最终还要遍历数组path,找到最小值返回,增加了时间;

      解决:如果从下向上迭代计算,每次计算都表示从当前位置出发,路径和的最小值,那么迭代到第一层时,就为要求的结果。

    新的解法:

    数组A初始值设为三角最底一层值,之后由下向上迭代三角。

    A[j]的意义为:保存从当前位置运动到三角最底层,所需要的最小路径和;

    而每次迭代的意义为:从当前位置,向下选择,选择下一层中,到达底端最小的路径,作为当前位置到达底端的最小路径;

    注,和思路一一样,需要在数组迭代中防止新旧值交叉,因为A[j] = triangle[i][j] + min(A[j], A[j+1]),因此从左向右计算新值较为合理。

    代码:

     1 class Solution {
     2 public:
     3     int minimumTotal(vector<vector<int> > &triangle) {
     4         int n = triangle.size();
     5         vector<int> path(triangle.back());
     6         
     7         for (int i = n - 2; i >= 0; --i) {
     8             for (int j = 0; j <= i; ++j) {
     9                 path[j] = triangle[i][j] + min(path[j], path[j+1]);
    10             }
    11         }
    12         
    13         return path[0];
    14     }
    15 };
  • 相关阅读:
    阿里云服务器绑定 微信公众号 服务器配置 问题记录
    Junit 报错: Failed to load ApplicationContext
    ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    idea 多项目部署JRebel
    sql优化1
    sql优化
    IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA搭建基础版的三层架构
    IntelliJ IDEA 2017版 spring-boot 报错Consider defining a bean of type 'xxx' in your configuration问题解决方案
    IntelliJ IDEA 2017版 spring-boot 实现jpa基本部署,通过实体类自动建立数据库
    Nginx的两种负载均衡搭建(Tomcat版)
  • 原文地址:https://www.cnblogs.com/huxiao-tee/p/4507283.html
Copyright © 2011-2022 走看看