LeetCode

来源:互联网 发布:服装设计用什么软件 编辑:程序博客网 时间:2024/06/10 16:40

62.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 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.

一个机器人到右下角摘星星,只能向右向下移动,问有多少条路可达。

一个dp题,每一个格子可从他的上边或者左边走过来,那么到达这个格子的路径数 = 到达他上边格子的路径数 + 到达他左边格子的路径数。时间复杂度O(m*n),空间复杂度O(m*n)

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

看到一种写法,回来补下题解,上面那种空间复杂度过高,可以降

因为每次只需要用到上一行的值,所以会发现我们只要开两个vector存上一行的值和这一行的值便足够了,不需要开整个二维空间存放。

class Solution {    int uniquePaths(int m, int n) {        if (m > n) return uniquePaths(n, m);         vector<int> pre(m, 1);        vector<int> cur(m, 1);        for (int j = 1; j < n; j++) {            for (int i = 1; i < m; i++)                cur[i] = cur[i - 1] + pre[i];            swap(pre, cur);        }        return pre[m - 1];    }};

然后发现,我们每次都覆盖了pre的值,就相当于,只用到了cur上一次存的值,这个时候我们可以省下pre数组的空间,只存cur即可。此时时间复杂度O(mn),空间复杂度(max(n,m))

class Solution {    int uniquePaths(int m, int n) {        if (m > n) return uniquePaths(n, m);        vector<int> cur(m, 1);        for (int j = 1; j < n; j++)            for (int i = 1; i < m; i++)                cur[i] += cur[i - 1];         return cur[m - 1];    }};


63. Unique Paths II

Follow up for "Unique Paths":

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.

在上一题的基础上添加了障碍物,思路跟上题一样,就是处理dp的时候,将障碍物存在的点ans[i][j]设为0,其余不变。时间复杂度O(m*n),空间复杂度O(m*n)

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

新写法,跟上一题第二种做法思路相同,时间复杂度O(mn),空间复杂度O(n)

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





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.

求一条从左上角到右下角沿途数字和最小的路径。延续了上面两题的新思路,依旧是只开O(n)的空间,来存每一行的结果(因为每一行的结果仅与上一行有关)。时间复杂度O(mn),空间复杂度O(n)

class Solution {public:    int minPathSum(vector<vector<int>>& grid) {        int m = grid.size();        int n = grid[0].size();        vector<int> ans(n, 0);        ans[0] = grid[0][0];        for (int i = 1; i < n; ++i) {            ans[i] = ans[i-1] + grid[0][i];        }        for (int i = 1; i < m; ++i) {            ans[0] += grid[i][0];            for (int j = 1; j < n; ++j) {                ans[j] = min(ans[j-1], ans[j]) + grid[i][j];            }        }        return ans[n-1];    }};