题目地址:
https://www.lintcode.com/problem/calculation-the-sum-of-path/description
给定一个 l l l行 w w w列的二维矩阵,从左上到右下角,规定必须经过某些点,并且每步只能向右或者向下走。问一共多少种走法。答案模 1 0 9 + 7 10^9+7 109+7返回。注意,题目里的下标都是从 1 1 1开始的。
先将所有的点从上到下从左到右排个序,然后两两之间算一下走法数,一个个乘上去即可。代码如下:
import java.util.Arrays;
public class Solution {
/**
* @param l: The length of the matrix
* @param w: The width of the matrix
* @param points: three points
* @return: The sum of the paths sum
*/
public long calculationTheSumOfPath(int l, int w, Point[] points) {
// Write your code here
Arrays.sort(points, (p1, p2) -> p1.x != p2.x ? Integer.compare(p1.x, p2.x) : Integer.compare(p1.y, p2.y));
Point start = new Point(0, 0), end = new Point(l - 1, w - 1);
long res = 0, MOD = (long) (1E9 + 7);
for (int i = 0; i < points.length; i++) {
Point point = points[i];
point.x--;
point.y--;
if (i == 0) {
res = compute(start, point, MOD);
} else {
res *= compute(points[i - 1], point, MOD);
}
res %= MOD;
}
res *= compute(points[points.length - 1], end, MOD);
return res % MOD;
}
private long compute(Point start, Point end, long MOD) {
int m = end.x - start.x + 1, n = end.y - start.y + 1;
// 可以滚动数组优化
long[][] dp = new long[2][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
dp[i & 1][j] = 1;
} else {
dp[i & 1][j] = dp[i - 1 & 1][j] + dp[i & 1][j - 1];
}
dp[i & 1][j] %= MOD;
}
}
return dp[m - 1 & 1][n - 1];
}
}
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
时间复杂度 O ( l w + p log p ) O(lw+p\log p) O(lw+plogp), p p p是点数,空间 O ( w ) O(w) O(w)。