前言

这里总结了两道表格移动的问题,分别是: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. HugePage简介和KVM中使用HugePage

    现在,有许多的处理器架构都支持多种的内存页大小,其中就包括比一般的page size大很多的huge page.就目前来说,尽管在个人电脑中基本都实现了对huge page的支持,然而,huge pa ...

  2. ubi实际使用

    ubifs号称性能比yaffs2 好,同时压缩可读写,文件系统image体较小同时可写.1. uboot使能对UBIFS的支持#define CONFIG_CMD_NAND#define CONFIG ...

  3. js实现点击定位最顶端

    //------------------------------------点击按钮------------------------------------ <span onClick=&quo ...

  4. struts2之constant 讲解 (转)

    struts.serve.static.browserCache 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false. stru ...

  5. eclipse中如何查看一个android模拟器的内部文件

    eclipse中如何查看一个android模拟器的内部文件,有时要在其中添加一个文件夹或是什么的,要手动的做这件事,而不能够用代码去完成时,就要用这个方法了. 1.首先,打开一个安卓模拟器. 2.这个 ...

  6. cocos2d-x中关于touch事件的响应

    原作者:有缘人  来源:新浪微博 地址:http://blog.sina.com.cn/s/blog_6ac2c7260102vvdu.html 一.touch事件响应分为单点触摸响应和多点触摸响应. ...

  7. Online Judge(字符串-格式)

    Online Judge Problem Description Ignatius is building an Online Judge, now he has worked out all the ...

  8. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  9. amortized analysis

    w https://en.wikipedia.org/wiki/Amortized_analysis In computer science, amortized analysis is a meth ...

  10. Hibernate 框架入门(一)

    1. SSH Web 层: Struts2 业务层: Spring 持久层: Hibernate 2. Hibernate 概述 概述 Hibernate 是一个对象关系映射框架(ORM 框架); 对 ...