[LeetCode] 62. Unique Paths_ 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 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 = ...
- [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 ...
- [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming
Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...
- [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 ...
随机推荐
- openfire课程
https://blog.csdn.net/huwenfeng_2011/article/category/2874473/2 https://www.cnblogs.com/Fordestiny/p ...
- FFmpeg libavutil主要功能概述
[时间:2017-08] [状态:Open] [关键词:ffmpeg,avutil,avrational,avlog,avbuffer,avoptoin] 0 引言 FFmpeg使用很久了,一直没有认 ...
- nginx转发成功报400 bad request,服务端未收到请求
nginx转发成功报400 bad request,服务端未收到请求 解决办法: upstream后面不要跟下划线 例如: upstream auth_service{ server 30.4.164 ...
- hdoj:2057
#include <cstdio> #include <cmath> #include <iostream> using namespace std; int ma ...
- C# 在while循环中new的对象
一: 问:那每次循环都会new一个A?那内存不是会满吗?还是说要把这个初始化对象的动作放到循环外面的写法会比较好? while(true) { A a = new A(); ... sleep(100 ...
- ABBYY PDF Transformer+支持的格式
ABBYY PDF Transformer+是一个新的,全面的巧妙解决PDF文档的工具,它将泰比的光学字符识别(OCR)技术和Adobe®PDF技术完美结合,以确保实现便捷地处理任何类型的PDF文件, ...
- Shell常见问题整理
1. 使用shell进行程序设计的原因是什么? 可以快速.简单的完成编程,非常适合于编写一些执行相对简单的任务的小工具.如果有一个简单的构想,可以通过它检查自己的想法是否可行.还可以使用shell对进 ...
- ArcGIS删除数据异常崩溃
环境 ArcGIS Desktop10.1,ArcSDE10.1,Oracle 11.2.0.4客户端和服务器端(服务器端搭建RAC集群) 问题描述 在使用ArcCatalog连接数据库进行数据删除时 ...
- Hystrix入门与分析(一):初识Hystrix
在以前的文章中,我们介绍过使用Gauva实现限流的功能,现在我们来了解一下如何在服务框架中实现熔断和降级的方法. 简介Hystrix 大型系统架构的演进基本上都是这样一个方向:从单体应用到分布式架构. ...
- vue---computed计算属性的使用
一直以来在使用vue的时候,会对vue的computed属性和watch属性具体的使用分不清楚,总算花点时间整理了下. computed:通常用于监控自己定义的变量,这个变量可以不再data中定义,直 ...