LeetCode - 62. Unique Paths
来源:互联网 发布:php开发管理系统 编辑:程序博客网 时间:2024/05/23 01:18
这道题是一道典型的动态规划题目,在题目中出现以下情况的话经常要考虑使用动态规划的方法:
而动态规划的话,通常会有以下几种类型:Matrix, Sequence, Two Sequences和Backpack。这道题目就是Matrix DP,而在Matrix DP中,有四个步骤需要考虑:
state: f[x][y]表示从起点走到坐标x,y...
function: 研究最后一步怎么走
initialize: 起点
answer: 终点
对于这道特定的题目,有以下的情况:
因为最后一步走法的数量等于从上面来的走法的数量+从左边来的走法的数量,另外需要注意的是,动态规划中用到二维数组的话,最好把[i][0]和[0][j]也进行初始化一下。时间复杂度为O(n^2),空间复杂度为O(m * n),代码如下:
public class Solution { public int uniquePaths(int m, int n) { int[][] matrix = new int[m][n]; // Initialize for(int i = 0; i < m; i++){ matrix[i][0] = 1; } for(int j = 0; j < n; j++){ matrix[0][j] = 1; } // DP for(int i = 1; i < m; i++){ for(int j = 1; j < n; j++){ matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1]; } } return matrix[m - 1][n - 1]; }}
知识点:
1. 使用动态规划的情况,以及培养动态规划的思想
2. 处理动态规划问题的四个要素,以及每一步所代表的意义
3. 动态规划中使用到二维数组的话,最好把[i][0], [0][j]也初始化一下
4. 注意这里题目中规定了只能向下或向右走,如果没有这个规定的话,那么不能使用动态规划去处理这个问题,因为我们没有办法去找function之间的关系
0 0
- [LeetCode]62.Unique Paths
- LeetCode --- 62. Unique Paths
- LeetCode 62.Unique Paths
- [Leetcode] 62. Unique Paths
- [leetcode] 62.Unique Paths
- [leetcode] 62.Unique Paths
- leetcode 62. Unique Paths
- Leetcode 62. Unique Paths
- [leetcode] 62. Unique Paths
- 62. Unique Paths LeetCode
- [LeetCode]62. Unique Paths
- 【LeetCOde】62. Unique Paths
- [LeetCode]62. Unique Paths
- leetcode 62. Unique Paths
- LeetCode 62. Unique Paths
- LeetCode *** 62. Unique Paths
- leetcode 62. Unique Paths
- Leetcode:62. Unique Paths
- 3Spring整合struts2
- 数据库中聚簇索引与非聚簇索引的区别
- this是指向对象本身的一个指针
- Codevs 1904 最小路径覆盖问题
- 4Spring MVC配置
- LeetCode - 62. Unique Paths
- 英国将迎来史上第二位女首相
- cookie入门级的使用
- #include<xxxx.h>与#include<sys/xxx.h>区别
- linux注销指定用户
- 5Spring事务
- SAP SD定价技术分析
- 6Spring AOP
- 现世斩