【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: 输入: ...
随机推荐
- python之filter()函数
filter()函数是python内置的一个高阶函数. filter()函数接受一个函数f 和一个list,这个函数f的作用是对每个元素进行判断,返回True或False,filter()根据判断结果 ...
- Photoshop脚本之创建文件夹
function checkFolder(path){ var folder = Folder(path) if(!folder.exists) folder.create() }
- Servlet 点击计数器
网页点击计数器 很多时候,您可能有兴趣知道网站的某个特定页面上的总点击量.使用 Servlet 来计算这些点击量是非常简单的,因为一个 Servlet 的生命周期是由它运行所在的容器控制的. 以下是实 ...
- jsp a href怎么传参数?
jsp中超链接传值使用键值对格式,多个键值对之间用&分隔,即<a href="show.jsp?name=tom&pass=123&score=78,5&quo ...
- Spring入门第一例
通过多天对基础语法的学习,早就向往一睹SPRING的芳容.今天按照ITEYE 唐的 教程,第一次运行Spring成功,步骤及注意事项如下: 一.基础环境 Jdk1.8, Eclipse4.71 .Sp ...
- hdu 4576(概率dp+滚动数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 思路:由于每次从某一位置到达另一位置的概率为0.5,因此我们用dp[i][j]表示第i次操作落在 ...
- Yii2 Queue队列
github地址:https://github.com/yiisoft/yii2-queue 问题:https://github.com/yiisoft/yii2-queue/issues Jobs ...
- 大话DFT频谱分析(并不是我的话)
有限长信号DFT结果的频谱泄露 提出问题 依照我们在"信号与系统"这门课建立的印象,不管如何频率的连续正弦信号,其频谱应当是两根笔直的谱线(含负频率) 但是,当我们把一段正弦信号採 ...
- php 工厂方法模式
#使用工厂方法模式是不知道要创建类的对象有哪些.interface IFactory{ public function CreateOperation();#工厂方法模式只有单个产品 } class ...
- splay tree旋转操作 hdu 1890
很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...