【LEETCODE】56、数组分类,适中级别,题目:62、63、1035

package y2019.Algorithm.array.medium;

/**
 * @ClassName UniquePathsWithObstacles
 * @Description TODO 63. Unique Paths II
 *
 * 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).
 * Now consider if some obstacles are added to the grids. How many unique paths would there be?
 *
 * Input:
 * [
 *   [0,0,0],
 *   [0,1,0],
 *   [0,0,0]
 * ]
 * Output: 2
 * Explanation:
 * There is one obstacle in the middle of the 3x3 grid above.
 * There are two ways to reach the bottom-right corner:
 * 1. Right -> Right -> Down -> Down
 * 2. Down -> Down -> Right -> Right
 *
 * 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
 * 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/unique-paths-ii
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * 网格中的障碍物和空位置分别用 1 和 0 来表示。
 *
 *
 * 1, 1, 1
 * 1, 0, 1
 * 1, 1, 2
 *
 * @Author xiaof
 * @Date 2019/7/15 22:00
 * @Version 1.0
 **/
public class UniquePathsWithObstacles {

    public int solution(int[][] obstacleGrid) {
        //这个题很有动态规划的倾向
        //上一个位置到最后一个位置有几个走法
        //a[i][j] = a[i - 1][j] + a[i][j -1] 分别只能向右向下
        int res[][] = new int[obstacleGrid.length][obstacleGrid[0].length];

        //初始化,如果遇到障碍,那么那个位置不可到达为0
        //左边只有一种走法,向下
        int h = 1,l = 1;
        for(int i = 0; i < obstacleGrid.length; ++i) {
            if(obstacleGrid[i][0] == 1) {
                h = 0;
            }
            res[i][0] =  h;
        }
        for(int j = 0; j < obstacleGrid[0].length; ++j) {
            if(obstacleGrid[0][j] == 1) {
                l = 0;
            }
            res[0][j] = l;
        }

        //进行动态规划
        for(int i = 1; i < obstacleGrid.length; ++i) {
            for(int j = 1; j < obstacleGrid[i].length; ++j) {
                res[i][j] = obstacleGrid[i][j] == 1 ? 0 : res[i - 1][j] + res[i][j - 1];
            }
        }

        return res[obstacleGrid.length - 1][obstacleGrid[obstacleGrid.length - 1].length - 1];

    }

    public static void main(String[] args) {
        int data[][] = {{0,0,0},{0,1,0},{0,0,0}};
        UniquePathsWithObstacles fuc = new UniquePathsWithObstacles();
        System.out.println(fuc.solution(data));
        System.out.println(data);
    }

}

 

package y2019.Algorithm.array.medium;

/**
 * @ClassName UniquePaths
 * @Description TODO 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?
 *
 * Input: m = 3, n = 2
 * Output: 3
 * Explanation:
 * From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
 * 1. Right -> Right -> Down
 * 2. Right -> Down -> Right
 * 3. Down -> Right -> Right
 *
 * 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
 * 问总共有多少条不同的路径?
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/unique-paths
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Author xiaof
 * @Date 2019/7/15 22:28
 * @Version 1.0
 **/
public class UniquePaths {

    public int solution(int m, int n) {
        //这个题很有动态规划的倾向
        //上一个位置到最后一个位置有几个走法
        //a[i][j] = a[i - 1][j] + a[i][j -1] 分别只能向右向下
        int res[][] = new int[m][n];

        //初始化,如果遇到障碍,那么那个位置不可到达为0
        //左边只有一种走法,向下
        int h = 1,l = 1;
        for(int i = 0; i < m; ++i) {
            res[i][0] =  h;
        }
        for(int j = 0; j < n; ++j) {
            res[0][j] = l;
        }

        //进行动态规划
        for(int i = 1; i < m; ++i) {
            for(int j = 1; j < n; ++j) {
                res[i][j] = res[i - 1][j] + res[i][j - 1];
            }
        }

        return res[m - 1][n - 1];
    }
}

 

package y2019.Algorithm.array.medium;

/**
 * @ClassName MaxUncrossedLines
 * @Description TODO 1035. Uncrossed Lines
 *
 * We write the integers of A and B (in the order they are given) on two separate horizontal lines.
 * Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that:
 * A[i] == B[j];
 * The line we draw does not intersect any other connecting (non-horizontal) line.
 * Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.
 * Return the maximum number of connecting lines we can draw in this way.
 *
 * Example 1:
 *
 * Input: A = [1,4,2], B = [1,2,4]
 * Output: 2
 * Explanation: We can draw 2 uncrossed lines as in the diagram.
 * We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.
 *
 * 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。
 * 现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且我们绘制的直线不与任何其他连线(非水平线)相交。
 * 以这种方法绘制线条,并返回我们可以绘制的最大连线数。
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/uncrossed-lines
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Author xiaof
 * @Date 2019/7/15 22:34
 * @Version 1.0
 **/
public class MaxUncrossedLines {

    public int solution(int[] A, int[] B) {
        //不能相交也就是用过的数前面就不能进行连接
        //每当A出i个数,B出j个数的时候,可以进行连接的情况是,当第i\和j的位置正好可以连线
        //如果不能,那么就分别加上A的i个数,和机上B的j个的时候取最大的一遍
        //res[i][j] = max{res[i - 1][j], res[i][j - 1]} or res[i][j] = res[i - 1][j - 1] + 1
        int m = A.length, n = B.length, dp[][] = new int[m + 1][n + 1];

        //初始化,默认为0
        for(int i = 1; i <= m; ++i) {
            //A使用几个数
            for(int j = 1; j <= n; ++j) {
                //这个循环代表B使用几个数
                if(A[i - 1] == B[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        return dp[m][n];
    }

}

 

posted @ 2019-07-15 23:15  cutter_point  阅读(386)  评论(0编辑  收藏  举报