【Unique Paths】cpp

题目:

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) {
            int dp[m][n];
            memset(dp, 0, sizeof(dp));
            for ( size_t i = 0; i < n; ++i ) dp[0][i] = 1;
            for ( size_t i = 0; i < m; ++i ) dp[i][0] = 1;
            for ( size_t i = 1; i < m; ++i )
            {
                for ( size_t j = 1; j < n; ++j )
                {
                    dp[i][j] = dp[i-1][j] + dp[i][j-1];
                }
            }
            return dp[m-1][n-1];
    }
};

tips:

常规dp解法。

=====================================

上面的代码有可以改进的地方:dp[m][n]并不用这些额外空间,只需要两个长度为n的数组即可;一个保存前一行的状态,一个用于遍历当前行的状态,每次滚动更新,可以省去额外空间。沿着上述思路改进了一版代码如下:

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

这个代码空间复杂度降到了O(n),但还是可以改进。其实只用一个一维的数组dp就可以了,代码如下。

class Solution {
public:
    int uniquePaths(int m, int n) {
            int curr[n];
            memset(curr, 0, sizeof(curr));
            curr[0] = 1;
            for ( size_t i = 0; i < m; ++i )
            {
                for ( size_t j = 1; j < n; ++j )
                {
                    curr[j] = curr[j-1] + curr[j];
                }
            }
            return curr[n-1];
    }
};

这里用到了滚动数组的技巧。有个细节需要注意,外层dp是可以从0行开始,省去了一部分代码。

时间: 2024-11-02 19:41:44

【Unique Paths】cpp的相关文章

leetcode 【 Unique Paths 】python 实现

题目: 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'

【Unique Paths II】cpp

题目: 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 m

leetcode 【 Unique Paths II 】 python 实现

题目: 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 m

【Sudoku Solver】cpp

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 代码: cla

【Gas Station】cpp

题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with

【Minimum Window】cpp

题目: Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such wind

【Permutations II】cpp

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 代码: class Solution { public: vector<vector<int>>

【Combination Sum 】cpp

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

【Word Break】cpp

题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true becau