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?

Solution:

Space: O(M * N)

  1. class Solution {
  2. public int uniquePaths(int m, int n) {
  3. int [] paths = new int[m][n];
  4. for (int i = 0; i < m; i++) {
  5. paths[i][0] = 1;
  6. }
  7. for (int i = 0; i < n; i++) {
  8. paths[0][i] = 1;
  9. }
  10. for (int i = 1; i < m; i++) {
  11. for (int j = 1; j < n; j++) {
  12. paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
  13. }
  14. }
  15. return paths[m - 1][n - 1];
  16. }
  17. }

Optimized Space to O(N)

  1. class Solution {
  2. public int uniquePaths(int m, int n) {
  3. if (m == 0 || n == 0) {
  4. return 0;
  5. }
  6. if (m == 1 ||n == 1) {
  7. return 1;
  8. }
  9. int [] paths = new int[n];
  10. Arrays.fill(paths, 1);
  11. for (int i = 1; i < m; i++) {
  12. for (int j = 1; j < n; j++) {
  13. paths[j] += paths[j - 1];
  14. }
  15. }
  16. return paths[n - 1];
  17. }
  18. }

[LC] 62. Unique Paths的更多相关文章

  1. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  2. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  3. [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 ...

  4. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  5. 62. Unique Paths

    题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...

  6. LeetCode OJ 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 ...

  7. 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(中等,我自己解出的第一道 DP 题^^)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

随机推荐

  1. 用Axure画原型图有感

    感觉前端做UE非常有优势啊- 但是在制作的时候,似乎陷入了误区: (1)只求原型图的漂亮,色彩丰富,忽略了其本质作用,是用来整理逻辑,画出逻辑流程的. (2)一开始就追求交互,高保真的原型,忽视了细节 ...

  2. Thread--使用condition实现顺序执行

    package condition; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Re ...

  3. LeetCode——230. 二叉搜索树中第K小的元素

    给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = ...

  4. 个性化bash

    zsh/on-my-zsh Ubuntu,deepin, 等可以使用  apt install 的系统  apt install zsh 一般就可以自动安装 RedHat(Fedora,Centos) ...

  5. POJ 3585 Accumulation Degree【换根DP】

    传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...

  6. SMO算法--SVM(3)

    SMO算法--SVM(3) 利用SMO算法解决这个问题: SMO算法的基本思路: SMO算法是一种启发式的算法(别管启发式这个术语, 感兴趣可了解), 如果所有变量的解都满足最优化的KKT条件, 那么 ...

  7. 把cifar数据转换为图片

    参考 https://gist.github.com/fzliu/64821d31816bce595a4bbd98588b37f5 """ make_cifar10.py ...

  8. vue 利用axios请求接口下载excel

    一般有三种方法: 方法一: 通过a标签下载 // href为文件的存储路径或者地址,download为问文件名 <a href="/images/download.jpg" ...

  9. 在维护项目中的UUID工具类

    import java.util.UUID; /** * <p> * Title:uuID生成器 * </p> * <p> * Description:UUID 标 ...

  10. platform 平台驱动——设备的写作流程

    说明:在内核源码里会有很多已经实现的驱动,对于我们来说只需要写好设备文件即可,但是我们如何知道驱动需要那些数据,以及有哪些驱动呢? 解决: 1.首先在内核源码目录下执行执行菜单配置命令: make m ...