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]; } }