zoukankan      html  css  js  c++  java
  • 62. 63. Unique Paths 64. Minimum Path Sum

    1.

    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 reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

    How many possible unique paths are there?

    Above is a 3 x 7 grid. How many possible unique paths are there?

    Note: m and n will be at most 100.

    class Solution {
    public:
        int uniquePaths(int m, int n) {
            vector<vector<int>> v(m, vector<int>(n, 0));
            int i, j;
            for(i = 0; i < m; i++)
            {
                for(j = 0; j < n; j++)
                {
                    if(0 == i || 0 == j)
                        v[i][j] = 1;
                    else
                        v[i][j] = v[i-1][j] + v[i][j-1];
                }
            }
            return v[m-1][n-1];
        }
    };

    2.

    Now consider if some obstacles are added to the grids. How many unique paths would there be?

    An obstacle and empty space is marked as 1 and 0 respectively in the grid.

    For example,

    There is one obstacle in the middle of a 3x3 grid as illustrated below.

    [
      [0,0,0],
      [0,1,0],
      [0,0,0]
    ]
    

    The total number of unique paths is 2.

    Note: m and n will be at most 100.

    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
            int m = obstacleGrid.size();
            if(m <= 0)
                return 0;
            int n = obstacleGrid[0].size();
            if(n <= 0 || obstacleGrid[0][0] == 1)
                return 0;
            vector<vector<int>> v(m, vector<int>(n, 0));
            int i, j;
            for(i = 0; i < m; i++)
            {
                for(j = 0; j < n; j++)
                {
                    if(obstacleGrid[i][j] == 1)
                        v[i][j] = 0;
                    else if(0 == i && j)
                        v[i][j] = v[0][j-1];
                    else if(0 == j && i)
                        v[i][j] = v[i-1][0];
                    else if(i && j)
                        v[i][j] = v[i-1][j] + v[i][j-1];
                    else
                        v[i][j] = 1;
                }
            }
            return v[m-1][n-1];
        }
    };

    3.

    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.

    int minPathSum(vector<vector<int> > &grid) {
        if (grid.size()<=0){
            return 0;
        }
        int i, j;
        for(i=0; i<grid.size(); i++){
            for(j=0; j<grid[i].size(); j++){
                int top = i-1<0 ? INT_MAX : grid[i-1][j] ;
                int left = j-1<0 ? INT_MAX : grid[i][j-1];
                if (top==INT_MAX && left==INT_MAX){
                    continue;
                }
                grid[i][j] += (top < left? top: left);
            }
        }
        return grid[grid.size()-1][grid[0].size()-1];
    }
  • 相关阅读:
    win10 uwp 装机必备应用 含源代码
    win10 uwp 装机必备应用 含源代码
    win10 uwp clone
    win10 uwp clone
    win10 输入法禁用IME
    win10 输入法禁用IME
    win10 输入法禁用IME
    查看SQL运行时间
    [学习笔记]三元环
    数据库范式
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5270183.html
Copyright © 2011-2022 走看看