Skip to content

Commit a6811d1

Browse files
committed
0064. Minimum Path Sum
1 parent 66c8cd2 commit a6811d1

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

markdown/0064. Minimum Path Sum.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
### [64\. Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
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.
7+
8+
**Note:** You can only move either down or right at any point in time.
9+
10+
**Example:**
11+
12+
```
13+
Input:
14+
[
15+
  [1,3,1],
16+
[1,5,1],
17+
[4,2,1]
18+
]
19+
Output: 7
20+
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
21+
```
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   public int minPathSum(int[][] grid) {
31+
       int width = grid[0].length;
32+
       int[] dp = new int[width]; // 表示到达该位置的最短路径长度
33+
       for (int i = 0; i < grid.length; i++) {
34+
           int[] ints = grid[i];
35+
           for (int j = 0; j < ints.length; j++) {
36+
               if (j == 0) {
37+
                   dp[0] += ints[j];
38+
              } else {
39+
                   if (i == 0) {
40+
                       dp[j] = dp[j - 1] + ints[j]; // 没有上,直接加
41+
                  } else {
42+
                       dp[j] = Math.min(dp[j - 1], dp[j]) + ints[j]; // 比较选择小的
43+
                  }
44+
              }
45+
          }
46+
      }
47+
       return dp[width - 1];
48+
  }
49+
}
50+
```
51+
![](https://raw.githubusercontent.com/coderbean/PicBed/master/20190718190913.png)

src/main/java/leetcode/_64_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._64_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[][] grid = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
10+
System.out.println(solution.minPathSum(grid));
11+
}
12+
}
13+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode._64_;
2+
3+
class Solution {
4+
public int minPathSum(int[][] grid) {
5+
int width = grid[0].length;
6+
int[] dp = new int[width]; // 表示到达该位置的最短路径长度
7+
for (int i = 0; i < grid.length; i++) {
8+
int[] ints = grid[i];
9+
for (int j = 0; j < ints.length; j++) {
10+
if (j == 0) {
11+
dp[0] += ints[j];
12+
} else {
13+
if (i == 0) {
14+
dp[j] = dp[j - 1] + ints[j]; // 没有上,直接加
15+
} else {
16+
dp[j] = Math.min(dp[j - 1], dp[j]) + ints[j]; // 比较选择小的
17+
}
18+
}
19+
}
20+
}
21+
return dp[width - 1];
22+
}
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
### [64\. Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
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.
7+
8+
**Note:** You can only move either down or right at any point in time.
9+
10+
**Example:**
11+
12+
```
13+
Input:
14+
[
15+
  [1,3,1],
16+
[1,5,1],
17+
[4,2,1]
18+
]
19+
Output: 7
20+
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
21+
```
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   public int minPathSum(int[][] grid) {
31+
       int width = grid[0].length;
32+
       int[] dp = new int[width]; // 表示到达该位置的最短路径长度
33+
       for (int i = 0; i < grid.length; i++) {
34+
           int[] ints = grid[i];
35+
           for (int j = 0; j < ints.length; j++) {
36+
               if (j == 0) {
37+
                   dp[0] += ints[j];
38+
              } else {
39+
                   if (i == 0) {
40+
                       dp[j] = dp[j - 1] + ints[j]; // 没有上,直接加
41+
                  } else {
42+
                       dp[j] = Math.min(dp[j - 1], dp[j]) + ints[j]; // 比较选择小的
43+
                  }
44+
              }
45+
          }
46+
      }
47+
       return dp[width - 1];
48+
  }
49+
}
50+
```
51+
![](https://raw.githubusercontent.com/coderbean/PicBed/master/20190718190913.png)

0 commit comments

Comments
 (0)