【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: 输入: ...
随机推荐
- 机器人的运动范围 剑指offer66题
include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...
- 【ASP.NET】——AdRotator控件
AdRotator控件即广告控件. 广告,是站点不可缺少的一部分.也是站点获取收益的最主要途径,但最初制作广告非常麻烦.asp.net就将该方法封装成了一个控件.为我们节省了非常多时间.这也是ASP. ...
- 着手打造你的随身系统---将linux装进移动硬盘
将Ubuntu等linux系统安装到移动硬盘--操作系统随身携带 前言 刚刚接触ubuntu,听说可以将linux系统安装到移动硬盘上,所以最近一周都在尝试将ubuntu安装到新买的移动 ...
- 大数据实战centos 6.7安装mysql5.7
https://www.cnblogs.com/jr1260/p/6590232.html
- go的sync.Map
sync.Map这个数据结构是线程安全的(基本类型Map结构体在并发读写时会panic严重错误),它填补了Map线程不安全的缺陷,不过最好只在需要的情况下使用.它一般用于并发模型中对同一类map结构体 ...
- go的map获取对应的key-value
场景: IP是个Key,string字符串是个值, 一个IP可以对应多个字符串. 代码如下: package main import ( "fmt" ) func main() { ...
- Prime pair connection (Project Euler 134)
题目大意: 对于连续的质数$p1$, $p2$, 满足$5 <= p1 <= 1000000$ 求出最小的整数$S$, 它以 $p1$结尾并且能够被$p2$整除. 求$S$的和. 思路: ...
- 页面加载,使用ajax查询某个类别,并且给它们添加(拼接)连接
直接上代码 $(function(){ var a = ''; $.get("productType1_findAll.action",function(data){ consol ...
- 七个迹象说明你可能受到APT 攻击
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXF1c2hp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- C2 CompilerThread0 如果抓到的java线程dump里占用CPU最高的线程是这个,99%可能是因为服务重启了
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f20c80b3800 nid=0x57c0 runnable ...