# 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?

Example 1:

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

Example 2:

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 -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down

Example 3:

Input: m = 3, n = 3
Output: 6

Constraints:

  • 1 <= m, n <= 100

It’s guaranteed that the answer will be less than or equal to 2 * 109.

1. 动态规划解法

class Solution {public int uniquePaths(int m, int n) {int[][] dp = new int[m][n];for (int i = 0; i < m; i++) dp[i][0] = 1;for (int i = 0; i < n; i++) dp[0][i] = 1;for (int r = 1; r < m; r++) {for (int c = 1; c < n; c++) {dp[r][c] = dp[r - 1][c] + dp[r][c - 1];}}return dp[m - 1][n - 1];}
}

2. 排列组合解法

这是一个组合问题,可以在没有 DP 的情况下解决。对于 mxn 网格,机器人必须准确地向下移动 m-1 步和向右移动 n-1 步,这些可以以任何顺序完成。

例如,给出的问题是 3x7 矩阵,机器人需要以任意顺序执行 2+6 = 8 步,其中 2 步向下,6 步向右。那不过是一个排列问题。向下表示为“D”,向右表示为“R”,以下是路径之一:-

DRRRDRRR

我们必须告诉上面给定单词的排列总数。因此,将 m & n 都减少 1 并应用以下公式:-

总排列 = (m+n)!/(m!* n!)

以下是我的代码做同样的事情:-

class Solution {public int uniquePaths(int m, int n) {if (m == 1 || n == 1) return 1;m--;n--;int hi = m > n ? m : n;int lo = m > n ? n : m;long r = 1;for (int i = 1, k = hi + 1; k <= hi + lo; i++, k++) {r = r * k / i;}return (int)r;}
}

参考

https://leetcode.com/problems/unique-paths/discuss/22953/Java-DP-solution-with-complexity-O(n*m)

https://leetcode.com/problems/unique-paths/discuss/22958/Math-solution-O(1)-space

算法: 唯一路径62. Unique Paths相关推荐

  1. [Leetcode]62. Unique Paths

    62. Unique Paths 本题难度: Easy Topic: Dynamic Programming Description A robot is located at the top-lef ...

  2. 【动态规划】LeetCode 62. Unique Paths

    LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...

  3. C#LeetCode刷题之#62-不同路径(Unique Paths)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3680 访问. 一个机器人位于一个 m x ...

  4. 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). ...

  5. [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). ...

  6. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  7. [swift] 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 ...

  8. 《每日一题》62. Unique Paths 不同路径

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 "Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为 &quo ...

  9. [dp] LeetCode 62. Unique Paths

    输入:两个int m和n 输出:一个int,表示不同路径的个数. 规则:有一个m行n列的矩阵,一个机器人从左上角走到右下角,每次向下或者向右走一格. 分析:目的是要找到从(0,0)到(m-1,n-1) ...

  10. 62. Unique Paths

    description: https://leetcode.com/problems/unique-paths/ 机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法 Note: E ...

最新文章

  1. gulp将多张小图自动合成雪碧图
  2. Net Framework 2.0 MSI returned error code 1603解决方法
  3. iOS架构-静态库.framework之依赖第三方库(7)
  4. 3大VR虚拟现实产品PK赛:HTC、Oculus、索尼谁是大哥
  5. 赋能 打造应对不确定性的敏捷团队 pdf_《赋能》:麦克里斯特尔教你打造应对不确定性的敏捷团队...
  6. 小米自动化运维平台演进设计思路
  7. 显式锁select for update 用法
  8. python重复元素判定_30段极简Python代码:这些小技巧你都Get了么
  9. python3获取网页内容_python3获取一个网页特定内容
  10. Unity 之 官网下载地址,方便各个版本的 Unity 安装包下载
  11. latex 去掉(不显示)空白页的页码与页眉
  12. 学堂在线笔记——前端与后台的故事——SQL语言及其编程
  13. id Software公司介绍
  14. 开发右脑,数字桩记忆扑克牌训练教程
  15. sinon.stub_JavaScript测试工具对决:Sinon.js vs testdouble.js
  16. Android 5.X 新特性详解(一)MD主题、Palette、视图阴影、Tinting(着色)和Clipping(裁剪)
  17. tick timer 间隔_adjtimex修改tick值用法举例
  18. 微型计算机原理与接口技术考试附录
  19. 新智元“元宇宙 新人类”论坛3月30日下午两点正式开始,嘉宾大咖云集.欢迎加入新智元首届元宇宙论坛群,诚邀小伙伴们相聚云端,一起探索「元宇宙」.
  20. Python中文转换成二进制、八进制、十六进制输出

热门文章

  1. headerIP php_PHP正确获取客户端IP地址
  2. scrapy的name变量_scrapy 如何设置全局变量?
  3. Nginx+Tomcat关于Session的管理
  4. 云端深度学习框架TensorFlow读取数据IO的高效方式
  5. 利用云服务器搭建内网映射服务器
  6. 面试 多线程 MFC CSDN
  7. leetcode 336. Palindrome Pairs
  8. Java实现生产者消费者问题与读者写者问题
  9. Android的Matrix 2
  10. android RelativeLayout 动态添加子View