[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 ...
随机推荐
- 【翻译】Apache Shiro10分钟教程
本文为翻译文,原文地址:http://shiro.apache.org/10-minute-tutorial.html 介绍 欢迎来到Apache Shiro的10分钟教程! 通过这个教程,你会明白一 ...
- git学习小游戏
学习git可以试试这个游戏:https://learngitbranching.js.org/
- win10下安装基于caffe的 Faster-Rcnn
安装教程 本篇博客将会教会你在Windows下配置py-faster-rcnn,请细心仔细阅读.说白了,Windows下配置这些东西就是一个坑. 安装配置Anaconda 由于py-faste ...
- 大话Json对象和Json字符串
一.Json对象和Json字符串的区别 (1)Json对象:可以通过javascript存取属性. 先介绍一下json对象,首先说到对象的概念,对象的属性是可以用:对象.属性进行调用的.例如: var ...
- Failed to execute 'write' on 'Document'动态载入的js不能执行write
统计代码一般都是直接一个标签,插入js,标签放在哪里,统计图表就放在哪里! 我现在是稍微改了一下,我自己加了一点js,在页面所有元素都加载完成之后我再动态的把统计js插入到我需要的地方. 统计代码的s ...
- 数据库更新锁WITH UPDLOCK
今天因为并发的问题,又讨论了一遍.之前以为同时两个线程开启,线程A加了更新锁,线程B没有加,线程A更新后,线程B也会继续下去代码.但是今天测试了一下,原来线程A更新后(解锁),线程B将不会继续,会出现 ...
- unity(2017.3) C# 常用API
1. 获取物体的 GetComponent playerRigidbody = GetComponent<Rigidbody> (); GetComponent<Animatro&g ...
- 微信小游戏下socket.io的使用
参考: 微信小游戏:socket.io 一 在微信小游戏 中使用socket.io报错 因为项目需求,后端要使用nodejs简单搭建一个服务器,通讯用json格式. 使用Egret提供的socket. ...
- mybatis14--注解的配置
去掉对应的mapper映射文件 在dao文件中增加注解 public interface StudentDao { /** * 新增学生信息 */ @Insert(value="insert ...
- oracle编码转换:AL32UTF8->ZHS16GBK
--修改Oracle数据库字符集为utf-8: SQL>conn / as sysdba; SQL>shutdown immediate; SQL>startup mount; SQ ...