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. (10) 如何MySQL读压力大的问题

    如何进行读写分离 由开发人员根据所执行的SQL类型连接不同的服务器 由数据库中间层实现读写分离 读写分离时,需要注意,对于实时性要求比较高的数据,不适合在从库上查询(因为主从复制存在一定延迟(毫秒级) ...

  2. [HDFS Manual] CH7 ViewFS Guide

    ViewFS Guide ViewFS Guide 1 介绍 2. The Old World(Prior to Federation) 2.1单个Namenode Clusters 2.2 路径使用 ...

  3. 【iCore4 双核心板_ARM】例程二十六:LWIP_MODBUS_TCP实验——电源监控

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  4. 北京Java笔试题整理

    北京Java笔试题整理 1.什么是java虚拟机?为什么ava被称作是"平台无关的编程语言? 答:Java虚拟机可以理解为一个特殊的"操作系统",只是它连接的不是硬件,而 ...

  5. laravel框架生產vender文件夹

    方法一.修改拓展 去php.ini中查看下面三个扩展项是否开启 extension=php_fileinfo.dll extension=php_mbstring.dll extension=php_ ...

  6. Dubbo 分布式 日志 追踪

    使用dubbo分布式框架进行微服务的开发,一个大系统往往会被拆分成很多不同的子系统,并且子系统还会部署多台机器,当其中一个系统出问题了,查看日志十分麻烦. 所以需要一个固定的流程ID和机器ip地址等来 ...

  7. 【Android Studio】Gradle配置及问题解决

    Error:Failed to open zip file.Gradle's dependency cache may be corrupt (this sometimes occurs after ...

  8. 第三天 Linux简单命令

    2018-5-22 15:21:59 使用 atom 可以在windows环境下同步代码与linux (汉化配置好就可以啦) 2018-4-13 18:09:31  该看32节啦 1.man +陌生命 ...

  9. PE、ELF结构图

    PE:https://bbs.pediy.com/thread-203563.htm ELF:https://blog.csdn.net/jiangwei0910410003/article/deta ...

  10. ArcGIS AddIN Sample学习笔记

    1.AddInEditorExtension 功能描述:编辑器扩展,实现在编辑要素,对编辑事件的监听,及对新创建的要素的处理 核心代码: void Events_OnStartEditing() { ...