zoukankan      html  css  js  c++  java
  • Minimum Path Sum <leetcode>

    Given a m x n 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.

    思路:看到这道题想到两种方法,第一种是分支限界,不过时间复杂度比较高,估计会超时,另一种方法是动态规划,时间复杂度为O(m*n,代码如下:

     1 class Solution {
     2 public:
     3     vector<vector<int>>  mi;
     4     int minPathSum(vector<vector<int> > &grid) {
     5         mi=grid;
     6         for(int i=1;i<grid[0].size();i++)
     7         {
     8             mi[0][i]=mi[0][i-1]+mi[0][i];
     9         }
    10         for(int i=1;i<grid.size();i++)
    11         {
    12             mi[i][0]=mi[i-1][0]+mi[i][0];
    13             for(int j=1;j<grid[0].size();j++)
    14             {
    15                 mi[i][j]=min(mi[i-1][j],mi[i][j-1])+mi[i][j];
    16             }
    17         }
    18         return mi[grid.size()-1][grid[0].size()-1];
    19     }
    20 };
  • 相关阅读:
    shell命令--stat
    英文段子
    OCP读书笔记(16)
    shell命令--uptime
    OCP读书笔记(15)
    shell命令--dmesg
    OCP读书笔记(14)
    shell命令--hostname
    OCP读书笔记(13)
    shell命令--uname
  • 原文地址:https://www.cnblogs.com/sqxw/p/3960163.html
Copyright © 2011-2022 走看看