【DP】 - Unique Paths

Given a grid[][], one can only move either right, right up or right down at any point in time, how many paths are there to start from top left corner and stop at top right corner?

class Solution {
    public int getNumOfPaths(int[][] grid) {
        if (grid == null || grid.length == 0) {
            return -1;
        }
      
        // dynamic programming
        int[][] paths = new int[grid.length][grid[0].length];
        paths[0][0] = 1;
        for (int i = 1; i < grid.length; i++) {
            paths[i][0] = 0;
        }

        for (int i = 0; i < grid.length; i++) {
            for (int j = 1; j < grid[0].length; j++) {
                if (i == 0) {
                    paths[i][j] = paths[i][j - 1] + paths[i + 1][j - 1];
                    continue;
                }

                paths[i][j] = paths[i][j - 1] + paths[i - 1][j - 1] + paths[i + 1][j - 1];
            }
        }
    } // end of for
    return paths[0][grid[0].length - 1];
}
Follow-up 1:

If three points of the grid are given, what is the result if it is asked to go through all these points.

Since one can only go from left to right, we could sort the given points in ascending order of j (from left to right), and go to each of the point in order, finally arrive at right top corner.

class Solution {

    int[][] paths;

    public getPaths(int[][] grid, int[] p1, int[] p2, int[] p3) {
        if (grid == null || grid.length == 0) {
            return -1;
        }

        int[][] points = new int[][]{{0, 0}, p1, p2, p3, {0, grid[0].length - 1}};
        Arrays.sort(points, Comparator.comparing((int[] arr) -> arr[1]));

        paths = new int[grid.length][grid[0].length];
        paths[0][0] = 1;
        for ( int i = 0; i < 4; i++) {
            getNumOfPaths(grid, points[i] , points[i + 1]);
        }

        return paths[0][grid[0].length - 1];
    }

    private void getNumOfPaths(int[][] grid, int[] s, int[] e) {
        // clear other paths on the same column with p1
        for (int i = 0; i < grid.length; i++) {
           if (i == s[0]) {
               continue;
           }
            paths[i][s[1]] = 0;
        }

        for (int i = 0; i < grid.length; i++) {
            for (int j = s[1] + 1; j <= e[1]; j++) {
                if (i == 0) {
                    paths[i][j] = paths[i][j - 1] + paths[i + 1][j - 1];
                    continue;
                }

                paths[i][j] = paths[i][j - 1] + paths[i - 1][j - 1] + paths[i + 1][j - 1];
            }
        }  // end of for
    }
}
Follow-up 2:

How to know if there can be any possible paths satisfy follow-up 1?

  1. Any two of the five points {{0, 0}, p1, p2, p3, {0, grid[0].length - 1}} should not have the same p[1];
  2. If any two points e.g. p1, p2 from the five points have |p1[1] - p2[1]| == 1, then |p1[0] - p2[0]| should also be 1.
Follow-up 3:

Given an H, make sure your path go beyond H to its down.

以h为对称轴,将右上角的点作关于h的轴对称。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • 在项目中引入react-native-camera报错: 解决方法:在项目的build.gradle文件中添加如下代码
    Thelastgame阅读 1,176评论 1 0
  • 天下没有免费的午餐? 美帝面试有三宝,流程清晰、费用全包、待遇好。 可千万别小看这三宝,勤劳刻苦、聪明过人的中国留...
    不是小羊的肖恩阅读 7,604评论 22 76
  • 今天,我的对桌美丽的姑娘小陈讲了一节试卷讲评课。 课,不好讲;讲得也确实不是非常非常棒。但是,这对于一个刚刚大学毕...
    夏花静秋阅读 991评论 3 4
  • 这两天为了面试奔波劳碌,初尝到求职的艰辛。但是还是遇到一些比较理想的公司,下面就和大家分享一下这几天的面试吧,坐标...
    kuohao阅读 10,230评论 1 19