62. Unique Paths (Graph; DP)
2024-08-22 12:43:18
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-];
}
};
最新文章
- iOS 支持 IPv6
- 递推 hdu 2064
- apk反编译
- nginx负载均衡基于ip_hash的session粘帖
- linux-yum
- linux socket
- Java的静态导入
- jquery iframe自适应高度[转]
- 简单搭建React-Native环境
- Spring 下载与安装以及spring 3.2.9 jar包详解
- 搭建一个Web应用
- javascript Klass 实现
- POJ1942——Paths on a Grid(组合数学)
- Android M(6.0) 权限爬坑之旅
- 设计模式的征途—20.备忘录(Memento)模式
- 爬虫入门 手写一个Java爬虫
- week1 - Python基础1 介绍、基本语法、流程控制
- [转]Examining Open vSwitch Traffic Patterns
- 最小生成树Prim算法和Kruskal算法
- BugPhobia开发篇章:Beta阶段第IX次Scrum Meeting