LeetCode 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.
题意:给出一个非负矩阵,求从左上角到右下角的路径和的最小值
思路:用dp(i,j)表示从(0,0)到(i,j)的路径的最小和,因为移动路径只能是向下和向右,状态转移方程为
dp(i,j)= min(dp(i-1,j) + grid[i][j], dp(i,j-1) + grid[i][j])
代码如下:
class Solution {public int minPathSum(int[][] grid){int row = grid.length;int col = row == 0 ? 0 : grid[0].length;int[][] temp = new int[row][col];for (int i = 0; i < row; i++){Arrays.fill(temp[i], Integer.MAX_VALUE);}temp[0][0] = grid[0][0];for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (i == 0 && j == 0) continue;if (i > 0){temp[i][j] = Math.min(temp[i][j], temp[i - 1][j] + grid[i][j]);}if (j > 0){temp[i][j] = Math.min(temp[i][j], temp[i][j - 1] + grid[i][j]);}}}return temp[row - 1][col - 1];}
}
LeetCode Minimum Path Sum(动态规划)相关推荐
- [leetcode] Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- LeetCode:Minimum Path Sum(网格最大路径和)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【DP】LeetCode 64. Minimum Path Sum
LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 63. Unique Paths II and 64. Minimum Path Sum
文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- [LeetCode]113.Path Sum II
[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...
- leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)
题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...
- LeetCode 113. Path Sum II
113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...
最新文章
- 2021年大数据ZooKeeper(六):ZooKeeper选举机制
- http 缓存分为客户端缓存和服务端缓存
- vs2010 常见问题处理
- java io 结构_java 的IO类库的基本架构
- .net对虚拟目录的操作
- C#如何获得系统时间
- gson json转map_Java几种常用JSON库性能比较
- matlab 多重循环在最外层加断点_循环优化之循环分块(loop tiling)
- 201触摸ic应用电路_有源电力滤波器的设计原理及应用前景-安科瑞蒋超萍
- 【无标题】自己动手写Docker系列 -- 6.3 手动配置容器网络(上)
- oracle搭建dblink,oracle创建dblink方法示例
- API不是从业务抽象出来的(1):设计思维
- MySQL服务器的启动与停止
- 2019美赛参赛获奖经验
- 【MATLAB】机器学习:线性判别分析LDA
- php生成各种验证码
- Bladex Workflow工作流引擎开发进阶-版本v1.2.2
- linux内核5.8.1,Linus Torvalds宣布大规模更新Linux内核5.8
- 六年级计算机机器人考试试题,小学六年级下册信息技术教案:机器人行走
- 深度linux 修复命令,修复启动
热门文章
- Git 学习(二)版本库创建
- Ubuntu安装Nginx+PHP+Memcache+Mysql指导手册
- 强烈推荐Spring Web Flow权威指南
- python简单代码 春节集五福-集五福活动又来了,不过这个价值几十亿的大项目也别错过...
- python自学多久可以找到工作-零基础如何自学Python并且找到工作,其实也就这3点,4点...
- python编程快速入门例子-清华大学出版社-图书详情-《Python编程入门与案例详解》...
- 十大python开发软件-5款开发安全、高质量代码的优秀Python工具
- 财务需要学python-8年老财务:财务分析学python就能提高效率?一半人是骗子
- 爬虫python需要什么软件-Python爬虫需要学习那些东西?
- python3.7.2下载-Python编程神器 v3.7.2 最新免费版