【leetcode】Minimum Path Sum(easy)

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.

思路:由于只能向两个方向走,瞬间就没有了路线迂回的烦恼,题目的难度大大的降低了。先得到到达最上边和最左边的最小路径,其他的就从上面点和左边点中选一个路径短的路径加上。

当然,也可以空间上更简单一些,只缓存一行的数据,然后一行行的更新。但我懒得写了...

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        if(grid.size() == 0)
        {
            return 0;
        }
        vector<vector<int> > ans(grid.size(), vector<int>(grid[0].size(), 0));
        ans[0][0] = grid[0][0];
        for(int i = 1; i < grid.size(); i++)
        {
            ans[i][0] = ans[i - 1][0] + grid[i][0];
        }
        for(int j = 1; j < grid[0].size(); j++)
        {
            ans[0][j] = ans[0][j - 1] + grid[0][j];
        }
        for(int i = 1; i <grid.size(); i++)
        {
            for(int j = 1; j <grid[0].size(); j++)
            {
                ans[i][j] = min(ans[i - 1][j], ans[i][j - 1]) + grid[i][j];
            }
        }
        return ans.back().back();
    }
};
时间: 2024-12-22 16:41:42

【leetcode】Minimum Path Sum(easy)的相关文章

【Leetcode】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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【LeetCode】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. [思路] 求从左上角到右下角的最小路径值,典型的动态规划

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8

【LeetCode】【Python】Minimum Path Sum

此题不难,可以用dfs来做,也可以用动态规划,但明显dfs性能不如dp. 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 ti

【数组】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. 思路: 设res[i][j]表示从左上角到grid[i][

【LeetCode】112 - Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【一天一道LeetCode】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22,

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

LeetCode --- 64. Minimum Path Sum

题目链接: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. 这道题的要求是在m*n