zoukankan      html  css  js  c++  java
  • 64. 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.

    Example:

    Input:
    [
      [1,3,1],
      [1,5,1],
      [4,2,1]
    ]
    Output: 7
    Explanation: Because the path 1→3→1→1→1 minimizes the sum.

    my code:

    class Solution {
    public:
        int minPathSum(vector<vector<int>>& grid) {
            int row = grid.size();
            int col = grid[0].size();
            vector<vector<int>> dp(row, vector<int>(col, 0));
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    if (i == 0 && j == 0)
                        grid[i][j] = grid[i][j];
                    else if (i == 0 && j != 0)
                        grid[i][j] = grid[i][j] + grid[i][j-1];
                    else if (j == 0 && i != 0)
                        grid[i][j] = grid[i][j] + grid[i-1][j];
                    else
                        grid[i][j] = grid[i][j] + min(grid[i-1][j], grid[i][j-1]);
                }
            }
            return grid[row-1][col-1];
        }
    };
    

    Runtime: 12 ms, faster than 20.71% of C++ online submissions for Minimum Path Sum.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    相关系数
    T检验
    Python模块常用的几种安装方式
    DOM与SAX读取XML方式的不同
    Base64编码
    node.js网页爬虫
    Node.js Express 框架
    Node.js Web 模块
    Node.js GET/POST请求
    Node.js 常用工具
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9824850.html
Copyright © 2011-2022 走看看