LeetCode 62 _ Unique Paths 全部不同路径

Description: 

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?


Above is a 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

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

Example 2:

Input: m = 7, n = 3
Output: 28

Solution: 

这道题让我们求出从规格为m*n的方格左上角走到右下角的所有不同路径总数

这道题很简单,我们知道,如果不走回头路,那么机器人只能向右或向下走,7*3的方格实际上要向右走6次,向下走2次

所以该问题本质是“m个A,n个B,有多少种不同排列”的高中概率论问题

求法即为在(m+n)个空位中选择m个空位给A,剩下的即为B的位置

答案即C62,通用解为(m+n)!/(m!*n!)

由于中间可能在乘分子的时候超过范围,因此中间值sum最好取double,以使结果的范围更大

Code:

public int uniquePaths(int m, int n) {
    if (m <= 0 && n <= 0){
        return 0;
    }

    int total = m + n - 2;
    int less = Math.min(m,n) - 1;
    double sum = 1;
    for (int i = 1; i <= less; i++){
        sum = sum * (total + 1 - i) / i;
    }
    return (int)sum;
}

  

提交情况:

Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Paths.

Memory Usage: 31.8 MB, less than 100.00% of Java online submissions for Unique Paths.

原文地址:https://www.cnblogs.com/zingg7/p/10655801.html

时间: 2024-10-05 00:52:04

LeetCode 62 _ Unique Paths 全部不同路径的相关文章

LeetCode 63 _ Unique Paths II 全部不同路径2

Description: 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

LeetCode 62, 63 Unique Paths I+II

62. Unique Paths 空间可以按行优化为 O(n),也可以按列优化为O(m). class Solution { public: int uniquePaths(int m, int n) { int dp[m][n]={0}; dp[0][0]=1; for (int i=0;i<m;++i){ for (int j=0;j<n;++j){ if (i==0&&j==0) continue; dp[i][j]=0; if (j-1>=0) dp[i][j]

LeetCode OJ: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

leetcode || 62、Unique Paths

problem: 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 'Fin

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

&amp;lt;LeetCode OJ&amp;gt; 62. / 63. Unique Paths(I / II)

62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: Medium 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

[LeetCode]20. 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 OJ:Unique Paths II(唯一路径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] 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