Jan 19 - Unique Paths; Array; DP;

first of all, we‘re goin to create a 2D array(int[][] grid) to store the number of paths to each position. we get the minimum value of m and n, mark it as min, then look through the row and column of grid[i][i] in each loop, when i == 0, we know we are looking through the toppest row and leftmost column in the array, and set the value of each element in the row and column to be 1. (This is because we can only go to the position in one unique path.) When 0<i<min, the value of the element is grid[i][j] = grid[i-1][j] + grid[i][j-1]; When the loop ends, we will finally get the value of grid[m-1][n-1], which the number of unique paths that we want to obtain.

Code:

public class Solution {
    public int uniquePaths(int m, int n) {
        int[][] grid = new int[m][n];
        if(m == 0 && n == 0){
            grid[m][n] = 1;
            return grid[m][n];
        }
        int min = m < n? m:n;
        int i = 0;
        for(; i < min; i++){
            if(i == 0){
                for(int j = 0; j < m; j++) grid[j][0] = 1;
                for(int j = 0; j < n; j++) grid[0][j] = 1;
            }
            else{
                for(int j = i; j < m; j++) grid[j][i] = grid[j-1][i] + grid[j][i-1];
                for(int j = i; j < n; j++) grid[i][j] = grid[i-1][j] + grid[i][j-1];
            }
        }
        return grid[m-1][n-1];
    }

}

  

时间: 2024-10-23 20:03:30

Jan 19 - Unique Paths; Array; DP;的相关文章

[LeetCode] Unique Paths II(DP)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

62. Unique Paths (Graph; DP)

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 t

[LeetCode]19. 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 t

Unique Paths II (dp题)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/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 rea

63. Unique Paths II (Graph; DP)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

[leetcode DP]63. Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

19.2.9 [LeetCode 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 t