文章目录

  • 1 63 Unique Paths II
    • 1.1 题目描述
    • 1.2 动态规划解决
  • 2 64. Minimum Path Sum
  • 2.1 题目理解
  • 2.2 动态规划

这一遍刷dp的题目就很轻松了。

1 63 Unique Paths II

1.1 题目描述

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).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and space is marked as 1 and 0 respectively in the grid.

输入:整数数组grid,表示一个mxn的方格,grid[i][j]=1表示是障碍,不能通过;grid[i][j]=0表示可以通过。
输出:能够从左上角走到右下角的不重复的路径数
规则:只能向下或者向右走

Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output: 2
Explanation: There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:

  1. Right -> Right -> Down -> Down
  2. Down -> Down -> Right -> Right

1.2 动态规划解决

class Solution {public int uniquePathsWithObstacles(int[][] obstacleGrid) {if(obstacleGrid[0][0]==1) return 0;int m = obstacleGrid.length;int n = obstacleGrid[0].length;int[][] dp = new int[m][n];dp[0][0] = (obstacleGrid[0][0]==1?0:1);for(int j=1;j<n;j++){dp[0][j]= (obstacleGrid[0][j]==1?0:dp[0][j-1]);}for(int i=1;i<m;i++){dp[i][0] = (obstacleGrid[i][0]==1?0:dp[i-1][0]);}for(int i=1;i<m;i++){for(int j=1;j<n;j++){dp[i][j] = (obstacleGrid[i][j]==0?dp[i-1][j]+dp[i][j-1]:0);}}return dp[m-1][n-1];}
}

2 64. Minimum Path Sum

2.1 题目理解

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.
输入:整数数组grid,表示一个mxn的方格,grid[i][j]表示通过方格的代价。
输出:能够从左上角走到右下角的最小代价
规则:只能向下或者向右走
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

2.2 动态规划

class Solution {public int minPathSum(int[][] grid) {int m = grid.length;int n = grid[0].length;int[][] dp = new int[m][n];dp[0][0] = grid[0][0];for(int i=1;i<m;i++){dp[i][0] = dp[i-1][0]+grid[i][0];}for(int j=1;j<n;j++){dp[0][j] = dp[0][j-1]+grid[0][j];}for(int i=1;i<m;i++){for(int j=1;j<n;j++){dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];}}return dp[m-1][n-1];}
}

63. Unique Paths II and 64. Minimum Path Sum相关推荐

  1. [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

    115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...

  2. 【动态规划】LeetCode 63. Unique Paths II

    LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...

  3. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

  4. LeetCode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. 63. Unique Paths II

    和Unique paths是一样的 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 2 if(obstacleGrid == ...

  6. [LeetCode]--63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 63. Unique Paths II 动态规划

    description: https://leetcode.com/problems/unique-paths/ 机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法,这个加了难度, ...

  8. 63. Unique Paths II 不同路径 II

    Title 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为 ...

  9. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

    1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

最新文章

  1. 基础知识——列表简介(二)
  2. JavaScript获取鼠标左键选中的内容
  3. C#类中的internal成员可能是一种坏味道
  4. 【深度学习】人脸识别模型的动手实践!
  5. boost::locale::generator用法的测试程序
  6. hadooppythonsql_python - hadoop,mapreduce demo
  7. Docker教程介绍
  8. 面试官,求求你不要问我这么简单但又刁难的算法题了
  9. 简单防止通过执行存储过程攻击服务器
  10. 解决Fail to create the java Virtual Machine
  11. java log4j 路径配置_指定log4j配置文件路径
  12. 单点登录原理与代码实现
  13. pygame 绘制爱心函数 r = 1-cos(θ). Tag: python | 图形界面 | GUI
  14. windows7改linux密码忘记了,Windows 7和Linux忘记了启动密码怎么办?-win7忘记开机密码...
  15. 常见的几种web api 接口加密签名模式
  16. 体系结构 记分牌实例
  17. 计算机休眠模式是关机吗,电脑中的待机、休眠、睡眠和关机状态的区别。
  18. ArrayList和LinkedList常用方法演示与解析
  19. 春招+秋招面试经历汇总,今天我们聊聊程序员应该进大厂还是国企(Java后端方向)
  20. 收藏这几个软件,让你轻松从视频里提取音频

热门文章

  1. VSCode.exe扩展主机意外终止。请重新加载窗口以恢复。
  2. Luogu P1115 最大子段和(dp 贪心)
  3. 微信小程序开发简易教程一
  4. mouseevent tips
  5. ubuntu小企鹅输fcitx入法乱码问题
  6. oracle调整Lock_sga参数而不使用虚拟内存
  7. 初中计算机课教什么时候,初中计算机教学课程教学方法探讨
  8. 项目集成Spring Security
  9. Spring Data JPA框架
  10. Android 上下滚动字幕实现