前言

这里总结了两道表格移动的问题,分别是: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数组更新的是线路上最小的权值和:递推公式如下:
 
 
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】【动态规划】表格移动问题的更多相关文章

  1. 快速上手leetcode动态规划题

    快速上手leetcode动态规划题 我现在是初学的状态,在此来记录我的刷题过程,便于以后复习巩固. 我leetcode从动态规划开始刷,语言用的java. 一.了解动态规划 我上网查了一下动态规划,了 ...

  2. [LeetCode] 动态规划入门题目

    最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法" ...

  3. LeetCode 动态规划

    动态规划:适用于子问题不是独立的情况,也就是各子问题包含子子问题,若用分治算法,则会做很多不必要的工作,重复的求解子问题,动态规划对每个子子问题,只求解一次将其结果保存在一张表中,从而避免重复计算. ...

  4. LeetCode动态规划题总结【持续更新】

    以下题号均为LeetCode题号,便于查看原题. 10. Regular Expression Matching 题意:实现字符串的正则匹配,包含'.' 和 '*'.'.' 匹配任意一个字符,&quo ...

  5. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  6. House Robber III leetcode 动态规划

    https://leetcode.com/submissions/detail/56095603/ 这是一道不错的DP题!自己想了好久没有清晰的思路,参看大神博客!http://siukwan.sin ...

  7. Leetcode 动态规划 - 简单

    1. 最大子序和 (53) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输 ...

  8. [Leetcode][动态规划] 第935题 骑士拨号器

    一.题目描述 国际象棋中的骑士可以按下图所示进行移动:                           我们将 “骑士” 放在电话拨号盘的任意数字键(如上图所示)上,接下来,骑士将会跳 N-1 步 ...

  9. [Leetcode][动态规划] 第931题 下降路径最小和

    一.题目描述 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示 ...

  10. [Leetcode][动态规划] 零钱兑换

    一.题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: ...

随机推荐

  1. Lucene.Net 介绍

    1 lucene简介1.1 什么是lucenepowered by 25175.netLucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desk ...

  2. 深度解析丨秒懂nova3手机上超酷炫的AR应用及开发

    此前在HUAWEI nova3发布会中,相信大家都已经感受到了AR能力带来的惊喜: 现实场景召唤圣斗士,随时随地交流合影: 点击观看视频:https://v.qq.com/x/page/m1344f6 ...

  3. 修改PHP session 默认时间方法

    修改三行如下: 1.session.use_cookies把这个的值设置为1,利用cookie来传递sessionid 2.session.cookie_lifetime这个代表SessionID在客 ...

  4. nagios 安装

    #!/bin/sh ################################################ #this scripts is created by oldboy #site: ...

  5. Spring MVC文本框

    以下示例显示如何使用Spring Web MVC框架在表单中使用文本框.首先使用Eclipse IDE来创建一个Web工程,按照以下步骤使用Spring Web Framework开发基于动态表单的W ...

  6. 2204 Problem A(水)

    问题 A: [高精度]被限制的加法 时间限制: 1 Sec  内存限制: 16 MB 提交: 54  解决: 29 [提交][状态][讨论版] 题目描述 据关押修罗王和邪狼监狱的典狱长吹嘘,该监狱自一 ...

  7. php 加入即时推送功能

    打开浏览器保持与服务器握手的websocket 之前用workerman接过很花时间,现在workerman对其代码做了优化->https://www.workerman.net/web-sen ...

  8. std::condition_variable(3)复习

    #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...

  9. poj1676(转换为二进制求解)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13175 题目关键:将0~9十个数转换为二进制数进行枚举比较 int ...

  10. commit Commit changes to stable storage 对变化提交

    Python36\site-packages\pymysql\connections.py # Python implementation of the MySQL client-server pro ...