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?

思路: 如果使用递归,那么DFS(i,j)=v[i][j]+max{DFS(i,j-1), DFS(i-1,j)},问题是DFS(i,j)可能会被计算两次,一次来自它右边的节点,一次来自它下面的节点。每个节点都被计算两次,时间复杂度就是指数级的了。解决方法是使用动态规划存储节点信息,避免重复计算。

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n]; dp[][] = ;
for(int i = ; i< n; i++ )
{
dp[][i] = ;
}
for(int i = ; i< m; i++ )
{
dp[i][] = ;
} for(int i = ; i< m; i++)
{
for(int j = ; j< n; j++)
{
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};

62. Unique Paths (Graph; DP)的更多相关文章

  1. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  2. leetcode-63. Unique Paths II · DP + vector

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

  3. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  4. [leetcode]62. Unique Paths 不同路径

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

  5. 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)

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

  6. [LeetCode] 62. Unique Paths 不同的路径

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

  7. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  8. 62. Unique Paths

    题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...

  9. LeetCode OJ 62. Unique Paths

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

随机推荐

  1. LARC Caffe笔记(一) Setup

    采用杂记的形式 从查找到知乎页面开始:https://www.zhihu.com/question/27982282 到mnist example页面:http://caffe.berkeleyvis ...

  2. Sql 基础问题

    Ref Projection and Selection 联结查询的原理(笛卡尔积) 设计 MySQL 数据表的时候一般都有一列为自增 ID,这样设计原因是什么,有什么好处?

  3. Swift 获取plist文件展示在TableView上

    // 1.定义二维数组 var data:[[String]]! override func viewDidLoad() { super.viewDidLoad() // 2.实例化tableView ...

  4. ubuntu 11.04 old sources.list

    #deb cdrom:[Ubuntu 11.04 _Natty Narwhal_ - Release amd64 (20110427.1)]/ natty main restricted # See ...

  5. CentOS6下yum升级安装mysql

    CentOS6默认版本的mysql是5.1.73,当前主流版本一般为mysql-5.6,需要安装该版本的话可以执行以下操作 1.1.卸载旧版mysql 1)备份数据 # 直接删除老版本的相关文件可能会 ...

  6. 使 docker 容器可以正常 mount privileged

    [docker]privileged参数 privileged参数   $ docker help run ... --privileged=false Give extended privilege ...

  7. 使用 Win2D 绘制带图片纹理的圆(或椭圆)

    使用 Win2D 绘制图片和绘制椭圆都非常容易,可是如何使用 Win2D 绘制图片纹理的椭圆呢? 本文内容 重力迷宫小球 Win2D 实现 关于 CanvasCommandList 重力迷宫小球 ▲ ...

  8. CSS 中文字体的英文名称 (simhei, simsun) 宋体 微软雅黑等

    Mac OS的一些:   华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 俪黑 P ...

  9. myeclipse越来越卡了怎么回事啊?

    去掉拼写检查:windows->preferences->General->Editors->Text Editors->Spelling 将“Enable spell ...

  10. PHP框架(如:laravel、yii2、thinkPHP5)中统一异常处理及统一日志打印

    背景: 现在写接口服务应用有一个很通用的需求,想通过日志.或者监控的形式监测的接口的运行情况,比如耗时.请求参数.响应结果.和前端联调接口时或者排查线上问题时日志必不可少,特别是现场日志. 应用运行时 ...