zoukankan      html  css  js  c++  java
  • Minimum Path Sum

    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.

    Analyse: dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];

    Runtime: 32ms.

     1 class Solution {
     2 public:
     3     int minPathSum(vector<vector<int>>& grid) {
     4         if(grid.size() == 0) return 0;
     5         int row = grid.size(); //row size
     6         int col = grid[0].size(); //column size
     7         
     8         vector<vector<int> > dp;
     9         dp = grid;
    10         for(int i = 1; i < row; i++) dp[i][0] = dp[i - 1][0] + grid[i][0];
    11         for(int j = 1; j < col; j++) dp[0][j] = dp[0][j - 1] + grid[0][j];
    12         
    13         for(int i = 1; i < row; i++){
    14             for(int j = 1; j < col; j++){
    15                 dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
    16             }
    17         }
    18         return dp[row - 1][col - 1];
    19     }
    20 };
  • 相关阅读:
    HDU 1060 Leftmost Digit
    HDU 1008 Elevator
    HDU 1042 N!
    HDU 1040 As Easy As A+B
    HDU 1007 Quoit Design
    欧拉函数
    HDU 4983 Goffi and GCD
    HDU 2588 GCD
    HDU 3501 Calculation 2
    HDU 4981 Goffi and Median
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4750117.html
Copyright © 2011-2022 走看看