【LeetCode】【动态规划】表格移动问题
前言
这里总结了两道表格移动的问题,分别是:Unique Paths 和
题一:Unique Paths
描述
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
思路:动态规划
dp[0][j] = 1
dp[i][0] = 1
dp[i][j] = dp[i -1][j] + dp[i][j-1]
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > dp(m ,vector<int>(n,));
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-];
}
};
题二:Minimum Path Sum
描述
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
思路
dp[0][0] = grid[0][0]
dp[i][0] = dp[i - 1][0] + grid[i][0]
dp[0][j] = dp[0][j-1] + grid[0][j]
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if(grid.size() == ) return ;
int n = grid.size(), m = grid[].size();
cout<<m<<" "<<n<<endl;
vector<vector<int> > dp(n, vector<int>(m, ));
for(int i = ;i<n;++i){
for(int j=;j<m;++j){
if(i == && j == )
dp[i][j] = grid[i][j];
else if(i == && j != )
dp[i][j] = dp[i][j-] + grid[i][j];
else if(j == && i != )
dp[i][j] = dp[i-][j] + grid[i][j];
else
dp[i][j] = min(dp[i-][j], dp[i][j-]) + grid[i][j];
}
}
return dp[n-][m-];
}
};
或者将for循环里的 if else提取出来分别处理:
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if(grid.size() == ) return ;
int n = grid.size(), m = grid[].size();
cout<<m<<" "<<n<<endl;
vector<vector<int> > dp(n, vector<int>(m, ));
dp[][] = grid[][];
for(int i = ;i<n;++i)
dp[i][] = dp[i - ][] + grid[i][];
for(int j = ;j<m;++j)
dp[][j] = dp[][j-] + grid[][j];
for(int i = ;i<n;++i){
for(int j= ;j<m;++j){
dp[i][j] = min(dp[i-][j], dp[i][j-]) + grid[i][j];
}
}
return dp[n-][m-];
}
};
【LeetCode】【动态规划】表格移动问题的更多相关文章
- 快速上手leetcode动态规划题
快速上手leetcode动态规划题 我现在是初学的状态,在此来记录我的刷题过程,便于以后复习巩固. 我leetcode从动态规划开始刷,语言用的java. 一.了解动态规划 我上网查了一下动态规划,了 ...
- [LeetCode] 动态规划入门题目
最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法" ...
- LeetCode 动态规划
动态规划:适用于子问题不是独立的情况,也就是各子问题包含子子问题,若用分治算法,则会做很多不必要的工作,重复的求解子问题,动态规划对每个子子问题,只求解一次将其结果保存在一张表中,从而避免重复计算. ...
- LeetCode动态规划题总结【持续更新】
以下题号均为LeetCode题号,便于查看原题. 10. Regular Expression Matching 题意:实现字符串的正则匹配,包含'.' 和 '*'.'.' 匹配任意一个字符,&quo ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- House Robber III leetcode 动态规划
https://leetcode.com/submissions/detail/56095603/ 这是一道不错的DP题!自己想了好久没有清晰的思路,参看大神博客!http://siukwan.sin ...
- Leetcode 动态规划 - 简单
1. 最大子序和 (53) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输 ...
- [Leetcode][动态规划] 第935题 骑士拨号器
一.题目描述 国际象棋中的骑士可以按下图所示进行移动: 我们将 “骑士” 放在电话拨号盘的任意数字键(如上图所示)上,接下来,骑士将会跳 N-1 步 ...
- [Leetcode][动态规划] 第931题 下降路径最小和
一.题目描述 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示 ...
- [Leetcode][动态规划] 零钱兑换
一.题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: ...
随机推荐
- active mq 配置
<transportConnectors> <!-- DOS protection, limit concurrent connections to 1000 and frame s ...
- Consul实现原理系列文章1: 用Raft来实现分布式一致性
工作中用到了Consul来做服务发现,之后一段时间里,我会陆续发一些文章来讲述Consul实现原理.在前一篇文章中,我介绍了Raft算法.这篇文章会讲讲Consul是如何使用Raft算法来实现分布式一 ...
- php 常面试
问题:请用最简单的语言告诉我PHP是什么? 回答:PHP全称:Hypertext Preprocessor,是一种用来开发动态网站的服务器脚本语言. 问题:什么是MVC? 回答:MVC由Model(模 ...
- Xcode调试项目时取消弹出框提示授权
问题2: instruments wants permission to analyze other processes.'DTServiceHub'需要控制另外一个进程,以便继续调试,键入密码以允许 ...
- CentOS 6.5 安装Gitlab 7.12.2
官网环境要求 参见:https://github.com/gitlabhq/gitlabhq GitLab is a Ruby on Rails application that runs on th ...
- In the shell, what does “ 2>&1 ” mean?
In a Unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulat ...
- asp.net 关于字符串内范围截取的一点方法总结
前两天有一位网友提出了一个字符串内截取字符串的问题,除了用普通的字符串截取的方式外,我推荐的是用LINQ方式来截取.两者实际上差别不是很大,都是采用字符串截取方式,但后者从写法和观察效果会比前者简单实 ...
- MySQL-库的操作
05-库的操作 本节重点: 掌握库的增删改查 一.系统数据库 执行如下命令,查看系统库 show databases; nformation_schema: 虚拟库,不占用磁盘空间,存储的是数 ...
- 《从零开始学Swift》学习笔记(Day 9)——离开表达式你试试!
原创文章,欢迎转载.转载请注明:关东升的博客 表达式啊是很重要地. 在Swift中,表达式有3种形式. 不指定数据类型 var a1 = 10 指定数据类型 var a1:Int = 10 使用分号 ...
- HTP 302 SEO作弊
w 李智慧