zoukankan      html  css  js  c++  java
  • LeetCode 64 Minimum Path Sum

    Problem:

    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.

    Summary:

    想要从m*n的整型数矩阵左上角走到右下角,每次只可以向右或向下移动一步,求路径上的整型数最小情况下的数字之和。

    Solution:

    1. 暴力法:枚举从左上角到右下角的所有路径,分别计算出路径和并比较。但效率过低且明显不可行,故不考虑。

    2. 动态规划:由于只可以向右或向下移动,若已知(i, j)位置上一格(i - 1, j)以及左一格(i, j - 1)的路径数字之和,即可确定该位置的路径数字和:

      dp[i][j] = min(dp[i - 1][j], dp[i][j - 1])

      初始化:dp[0][0] = grid[0][0]

          dp[0][j] = dp[0][j - 1] + grid[0][j]

          dp[i][0] = dp[i - 1][0] + grid[i][0]

     1 class Solution {
     2 public:
     3     int minPathSum(vector<vector<int>>& grid) {
     4         int row = grid.size(), col = grid[0].size();
     5         vector<vector<int>> dp(row, vector<int>(col));
     6         
     7         for (int i = 0; i < row; i++) {
     8             for (int j = 0; j < col; j++) {
     9                 if (!i && !j) {
    10                     dp[i][j] = grid[i][j];
    11                 }
    12                 else if (!i && j) {
    13                     dp[i][j] = dp[i][j - 1] + grid[i][j];
    14                 }
    15                 else if (i && !j) {
    16                     dp[i][j] = dp[i - 1][j] + grid[i][j];
    17                 }
    18                 else {
    19                     dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
    20                 }
    21             }
    22         }
    23         
    24         return dp[row - 1][col - 1];
    25     }
    26 };

    3. 在法2的基础上优化空间

     1 class Solution {
     2 public:
     3     int minPathSum(vector<vector<int>>& grid) {
     4         int row = grid.size(), col = grid[0].size();
     5         vector<int> dp(col);
     6         
     7         dp[0] = grid[0][0];
     8         for (int i = 0; i < row; i++) {
     9             for (int j = 0; j < col; j++) {
    10                 if (!i && !j) {
    11                     dp[j] = grid[i][j];
    12                 }
    13                 else if (!i && j) {
    14                     dp[j] = dp[j - 1] + grid[i][j];
    15                 }
    16                 else if (i && !j) {
    17                     dp[j] += grid[i][j];
    18                 }
    19                 else {
    20                     dp[j] = min(dp[j], dp[j - 1]) + grid[i][j];
    21                 }
    22             }
    23         }
    24         
    25         return dp[col - 1];
    26     }
    27 };
  • 相关阅读:
    Python中的类(上)
    Django REST Framework API Guide 07
    Django REST Framework API Guide 06
    Django REST Framework API Guide 05
    Django REST Framework API Guide 04
    Django REST Framework API Guide 03
    Django REST Framework API Guide 02
    Django REST Framework API Guide 01
    Django 详解 信号Signal
    Django 详解 中间件Middleware
  • 原文地址:https://www.cnblogs.com/VickyWang/p/6240818.html
Copyright © 2011-2022 走看看