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

这个题目思路就是dynamic programming, A[i][j] = A[i-1][j] + A[i][j-1] , i, j >= 1.     T: O(m*n)  , S: O(n)   optimal(用滚动数组)

1. Constraints.

1) size [0*0] - [100*100]

2) edge case, m==0 or n ==0 => 0

2. Ideas

DP    T: O(m*n)   S; O(m*n)

3. Codes

1)  T: O(m*n)  , S: O(m*n)

 class Solution:
def uniquePaths(self, m, n):
if m == 0 or n == 0: return
ans = [[1]*n for _ in range(m)]
for i in range(1, m):
for j in range(1, n):
ans[i][j] = ans[i-1][j] + ans[i][j-1]
return ans[-1][-1]
# or return ans[m-1][n-1]

2) T: O(m*n)  , S: O(n)   滚动数组

 class Solution:
def uniquePaths(self, m, n):
if m == 0 or n == 0: return
ans = [[1] *n for _ in range(2)] # 模2
for i in range(1, m):
for j in range(1, n):
ans[i%2][j] = ans[i%2-1][j] + ans[i%2][j-1]
return ans[(m-1)%2][n-1]

4. Test cases

1) edge cases

2) 7, 3

[LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

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

  4. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  6. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  7. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  8. [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming

    Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...

  9. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

随机推荐

  1. [HDFS Manual] CH1 HDFS体系结构

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  2. xml序列化与反序列化工具

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  3. js快速排序算法

    真正的快速排序算法一: function quickSort(array){ function sort(prev, numsize){ var nonius = prev; var j = nums ...

  4. 【资料下载区】【GMT43相关代码、资料下载地址】更新日期2017/06/28

    [GMT43相关文档][更新中...] GMT43原理图(PDF)下载GMT43说明书(PDF)下载GMT43机械结构尺寸(PDF)下载 [GMT43相关例程代码][ARM][更新中...] 基于HA ...

  5. php cli模式下获取参数的方法

    转载声明:http://blog.csdn.net/fdipzone/article/details/51945892 php在cli模式下接收参数有两种方法 1.使用argv数组 <?php ...

  6. Socket端口复用

    在网络应用中(如Java Socket Server),当服务关掉立马重启时,很多时候会提示端口仍被占用(因端口上有处于TIME_WAIT的连接).此时可通过 SO_REUSEADDR 参数( soc ...

  7. Variational RL for POMDP

    1.Le, Tuan Anh, et al. "Auto-encoding sequential monte carlo." arXiv preprint arXiv:1705.1 ...

  8. 程序员自己编写的类和JDK类是一种合作关系。

    封装类: JAVA为每一个简单数据类型提供了一个封装类,使每个简单数据类型可以被Object来装载. 除了int和char,其余类型首字母大写即成封装类. 转换字符的方式: int I=10; Str ...

  9. 聊聊IOCP,聊聊异步编程

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  10. opencv利用直方图判断人脸光照质量

    懒得用中文再写一遍了, 直接传送门过去吧. https://medium.com/@fanzongshaoxing/detect-face-in-bad-lighting-condition-usin ...