【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 time.

动态规划:

用dis[x][y]表示到达(x,y)的最短距离,因为只能向右和向下,所以dis(x,y) = dis[x-1][y]和dis[x][y-1]的较小值加上A(x,y)即可。最终所求转化为dis(m-1,n-1)

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        int mm=grid.size();
        int nn=grid[0].size();
        vector<vector<int> > dis;
        vector<int> vec;
        vec.push_back(grid[0][0]);
        for (int i=1;i<nn;i++){
            vec.push_back(vec[i-1]+grid[0][i]);
        }
        dis.push_back(vec);
        for (int i=1;i<mm;i++){
            vec.clear();
            for (int j=0;j<nn;j++){
                if (j==0)   vec.push_back(dis[i-1][j]+grid[i][j]);
                else    vec.push_back(min(dis[i-1][j],vec[j-1])+grid[i][j]);
            }
            dis.push_back(vec);
        }
        return dis[mm-1][nn-1];
    }
};

除此,还应注意很多细节,几次提交都不能通过就是因为没处理好vec和dis的关系。好久没用c++做题,vector还没有push值的时候是不能通过直接访问下标进行赋值的,这一点需要特别注意。

时间: 2024-07-30 22:33:33

【LeetCode】【Python】Minimum Path Sum的相关文章

【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刷题笔记】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刷题笔记】Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

【leetcode刷题笔记】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 (

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

【leetcode刷题笔记】Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

【leetcode刷题笔记】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和