[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 ...
随机推荐
- 【翻译】Nginx的反向代理
本文为翻译文,原文地址:https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ 本文描述代理服务器的基本配置.你能学到如何 ...
- Oracle sql 统计
1.按小时统计数量 SELECT TO_CHAR(RECEIVE_TIME,'HH24') HOUR ,COUNT(*) N FROM PH_PRESCRIPTION P WHERE TO_CHAR( ...
- [转]list的交集,差集,并集
原文地址:https://www.cnblogs.com/changfanchangle/p/8966860.html 工作中用到了list的取差集,发现还是挺好用的.所以记录下. 需求 list的方 ...
- Java编程的逻辑 (89) - 正则表达式 (中)
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- [转]git commit --amend用法
适用场景: 比方说,你的代码已经提交到git库,leader审核的时候发现有个Java文件代码有点问题,于是让你修改,通常有2种方法: 方法1:leader 将你提交的所有代码 abandon掉,然后 ...
- 【算法】八皇后问题 Python实现
[八皇后问题] 问题: 国际象棋棋盘是8 * 8的方格,每个方格里放一个棋子.皇后这种棋子可以攻击同一行或者同一列或者斜线(左上左下右上右下四个方向)上的棋子.在一个棋盘上如果要放八个皇后,使得她们互 ...
- [转]SASS用法指南
[转]SASS用法指南 转自阮一峰 SASS用法指南 一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了 ...
- JS对象与Dom对象与jQuery对象之间的区别
前言 通过问题看本质: 举例: js的写法:document.getElementById('save').disabled=true; 在jquery中我是这样写的 $("#save&qu ...
- 【Excel】输出CSV文本
'******************************************************************************* ' CSV形式テキストファイル書き出す ...
- 转:SQL Server 动态行转列
http://www.cnblogs.com/gaizai/p/3753296.html http://www.cnblogs.com/maanshancss/archive/2013/03/13/2 ...