LintCode: Minimum Path Sum

C++,

time: O(m*n)

space: O(m*n)

 1 class Solution {
 2 public:
 3     /**
 4      * @param grid: a list of lists of integers.
 5      * @return: An integer, minimizes the sum of all numbers along its path
 6      */
 7     int minPathSum(vector<vector<int> > &grid) {
 8         // write your code here
 9         int m = grid.size(), n = grid[0].size();
10         vector<vector<int> > dp(m, vector<int>(n));
11         for (int i = 0; i < m; i++) {
12             for (int j = 0; j < n; j++) {
13                 if (i == 0) {
14                     if (j == 0) {
15                         dp[i][j] = grid[i][j];
16                     } else {
17                         dp[i][j] = dp[i][j-1] + grid[i][j];
18                     }
19                 } else if (j == 0) {
20                     dp[i][j] = dp[i-1][j] + grid[i][j];
21                 } else {
22                     dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
23                 }
24             }
25         }
26         return dp[m-1][n-1];
27     }
28 };

C++,

time: O(m*n)

space: O(m)

 1 class Solution {
 2 public:
 3     /**
 4      * @param grid: a list of lists of integers.
 5      * @return: An integer, minimizes the sum of all numbers along its path
 6      */
 7     int minPathSum(vector<vector<int> > &grid) {
 8         // write your code here
 9         int m = grid.size(), n = grid[0].size();
10         vector<int> dp(n);
11         for (int i = 0; i < m; i++) {
12             for (int j = 0; j < n; j++) {
13                 if (i == 0) {
14                     if (j == 0) {
15                         dp[j] = grid[i][j];
16                     } else {
17                         dp[j] = dp[j-1] + grid[i][j];
18                     }
19                 } else if (j == 0) {
20                     dp[j] = dp[j] + grid[i][j];
21                 } else {
22                     dp[j] = min(dp[j], dp[j - 1]) + grid[i][j];
23                 }
24             }
25         }
26         return dp[n-1];
27     }
28 };
时间: 2024-11-04 23:17:16

LintCode: Minimum Path Sum的相关文章

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

【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] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to rea

LeetCode: 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. SOLUTION 1: 相当基础

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

Leetcode: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. 解题分析: 每次只能向下或者向

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/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 r

leetcode_64题——Minimum Path Sum(动态规划)

Minimum Path Sum Total Accepted: 38669 Total Submissions: 120082My Submissions Question Solution 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. Not

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. 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 righ