友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

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.
0064 1
0064 2

思考题:尝试使用一维数组做备忘录来实现一下。

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.

思路分析

  • 一刷

  • 二刷

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Runtime: 7 ms, faster than 5.53% of Java online submissions for Minimum Path Sum.
 * Memory Usage: 43.5 MB, less than 33.78% of Java online submissions for Minimum Path Sum.
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-27 21:29
 */
public int minPathSum(int[][] grid) {
    if (Objects.isNull(grid) || grid.length == 0) {
        return 0;
    }
    int yLength = grid.length;
    int xLength = grid[0].length;

    for (int y = 0; y < yLength; y++) {
        for (int x = 0; x < xLength; x++) {
            if (y == 0 && x > 0) {
                grid[y][x] = grid[y][x - 1] + grid[y][x];
            } else if (y > 0 && x == 0) {
                grid[y][x] = grid[y - 1][x] + grid[y][x];
            } else if (y > 0 && x > 0) {
                grid[y][x] = Math.min(grid[y - 1][x], grid[y][x - 1]) + grid[y][x];
            }
        }
    }

    return grid[yLength - 1][xLength - 1];
}

/**
 * Runtime: 5 ms, faster than 7.69% of Java online submissions for Minimum Path Sum.
 * Memory Usage: 43.2 MB, less than 36.48% of Java online submissions for Minimum Path Sum.
 */
public int minPathSumDp(int[][] grid) {
    if (Objects.isNull(grid) || grid.length == 0) {
        return 0;
    }
    int yLength = grid.length;
    int xLength = grid[0].length;

    int[][] sums = new int[yLength][xLength];

    for (int i = 1; i < sums.length; i++) {
        Arrays.fill(sums[i], Integer.MAX_VALUE);
    }

    sums[0][0] = grid[0][0];
    for (int x = 1; x < xLength; x++) {
        sums[0][x] = sums[0][x - 1] + grid[0][x];
    }
    for (int y = 1; y < yLength; y++) {
        sums[y][0] = sums[y - 1][0] + grid[y][0];
    }
    for (int y = 1; y < yLength; y++) {
        for (int x = 1; x < xLength; x++) {
            sums[y][x] = Math.min(sums[y - 1][x], sums[y][x - 1]) + grid[y][x];
        }
    }
    return sums[yLength - 1][xLength - 1];
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-27 21:29
 */
public int minPathSum(int[][] grid) {
  int[][] dp = new int[grid.length][grid[0].length];
  dp[0][0] = grid[0][0];
  for (int i = 1; i < grid[0].length; i++) {
    dp[0][i] = grid[0][i] + dp[0][i - 1];
  }
  for (int i = 1; i < grid.length; i++) {
    dp[i][0] = grid[i][0] + dp[i - 1][0];
  }
  for (int row = 1; row < grid.length; row++) {
    for (int column = 1; column < grid[0].length; column++) {
      dp[row][column] = Math.min(dp[row - 1][column], dp[row][column - 1])
        + grid[row][column];
    }
  }
  return dp[grid.length - 1][grid[0].length - 1];
}