2014
11-18

LeetCode-Unique Paths[DFS]

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.

1. 深搜

// LeetCode, Unique Paths
// 深搜，小集合可以过，大集合会超时
// 时间复杂度O(n^4)，空间复杂度O(n)
class Solution {
public:
int uniquePaths(int m, int n) {
if (m < 1 || n < 1) return 0; // 终止条件

if (m == 1 && n == 1) return 1; // 收敛条件

return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
}
};

2. 备忘录法

// LeetCode, Unique Paths
// 深搜 + 缓存，即备忘录法
// 时间复杂度O(n^2)，空间复杂度O(n^2)
class Solution {
public:
int uniquePaths(int m, int n) {
// 0行和0列未使用
this->f = vector<vector<int> >(m + 1, vector<int>(n + 1, 0));
return dfs(m, n);
}
private:
vector<vector<int> > f;  // 缓存

int dfs(int x, int y) {
if (x < 1 || y < 1) return 0; // 数据非法，终止条件

if (x == 1 && y == 1) return 1; // 回到起点，收敛条件

return getOrUpdate(x - 1, y) + getOrUpdate(x, y - 1);
}

int getOrUpdate(int x, int y) {
if (f[x][y] > 0) return f[x][y];
else return f[x][y] = dfs(x, y);
}
};

3. 动态规划

f[i][j]=f[i-1][j]+f[i][j-1]

// LeetCode, Unique Paths
// 动规，滚动数组
// 时间复杂度O(n^2)，空间复杂度O(n)
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> f(n, 0);
f[0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 1; j < n; j++) {
// 左边的f[j]，表示更新后的f[j]，与公式中的f[i[[j]对应
// 右边的f[j]，表示老的f[j]，与公式中的f[i-1][j]对应
f[j] = f[j - 1] + f[j];
}
}
return f[n - 1];
}
};

4. 数学公式

// LeetCode, Unique Paths
// 数学公式
class Solution {
public:
typedef long long int64_t;
// 求阶乘, n!/(start-1)!，即 n*(n-1)...start，要求 n >= 1
static int64_t factor(int n, int start = 1) {
int64_t  ret = 1;
for(int i = start; i <= n; ++i)
ret *= i;
return ret;
}
// 求组合数 C_n^k
static int64_t combination(int n, int k) {
// 常数优化
if (k == 0) return 1;
if (k == 1) return n;

int64_t ret = factor(n, k+1);
ret /= factor(n - k);
return ret;
}

int uniquePaths(int m, int n) {
// max 可以防止n和k差距过大，从而防止combination()溢出
return combination(m+n-2, max(m-1, n-1));
}
};

Unique Paths II

Minimum Path Sum