当前位置:网站首页>64. Minimum path sum: given an M x n grid containing non negative integers, please find a path from the upper left corner to the lower right corner, so that the sum of the numbers on the path is the m
64. Minimum path sum: given an M x n grid containing non negative integers, please find a path from the upper left corner to the lower right corner, so that the sum of the numbers on the path is the m
2022-07-19 04:16:00 【? abc!】
Title Description
Given a... That contains a nonnegative integer m x n grid grid , Please find a path from the top left corner to the bottom right corner , Make the sum of the numbers on the path the smallest .
explain : You can only move down or right one step at a time .

Ideas
Use dynamic programming to solve :
- The direction of the path can only be down or right
- First row of grid Each element of can only be moved to the right from the upper left corner to , The first column of the grid Each element of can only be moved down from the upper left corner to , At this time The path is unique , Therefore, the sum of the minimum path corresponding to each element is the sum of the numbers on the corresponding path
- Elements that are not in the first row or column : With Move down one step from the adjacent element above arrive , perhaps Move one step to the right from its left adjacent element arrive , The minimum path corresponding to the element and be equal to
The minimum value in the sum of the minimum path corresponding to the upper adjacent element and its left adjacent element plus the value of the current element - legend : This is the first row and the first column ; The original array corresponds to dp Array is the synthesis of numbers on the corresponding path

- as follows , Not in the first row and column ; This corresponds to dp The elements of the array are The minimum value in the sum of the minimum path corresponding to the upper adjacent element and its left adjacent element plus the value of the current element

6. It can be seen from the above analysis , The equation is as follows :
Code
class Solution {
public int minPathSum(int[][] grid) {
// If grid Array is empty
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
}
// Get row and column
int rows = grid.length,clumns = grid[0].length;
// Create target array
int[][] dp = new int[rows][clumns];
// Assign values to the first row and first column elements
dp[0][0] = grid[0][0];
// On the first line
for(int i=1;i<rows;i++){
dp[i][0] = dp[i-1][0] + grid[i][0];
}
// In the first column
for(int j=1;j<clumns;j++){
dp[0][j] = dp[0][j-1] + grid[0][j];
}
// Not the first row and the first column
// Circular row
for(int i=1;i<rows;i++){
// Circular column
for(int j=1;j<clumns;j++){
// Get the smallest value of the current element of the target array
dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[rows-1][clumns-1];
}
}
边栏推荐
- High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
- Idea configures SFTP and SSH, which is very convenient to deploy and locate error logs
- Wechat e-book reading applet graduation design of applet completion works (2) applet function
- minimum spanning tree
- Typescript数组/对象/字符串/函数参数的解构使用
- To build agile teams, these methods are indispensable
- 【黄啊码】MySQL入门—5、数据库小技巧:单个列group by就会,多个列呢?
- 若依框架包名修改器
- [database] must know and be able at the end of the term ----- Chapter 11 concurrency control
- Introduction to Maui framework 05 MVVM data model understanding
猜你喜欢

Openresty 做静态资源服务器

机器学习09:无监督学习

Chapter 3 performance platform godeye source code analysis - memory module

【Notebook系列第七期】OpenVINO预训练模型的的下载和使用方法

机器学习11:代价敏感学习

【黄啊码】MySQL入门—5、数据库小技巧:单个列group by就会,多个列呢?

donet framework4.X==windows窗体应用新建项目,通过System.Data.SqlClient连接sqlserver进行查询

Openresty as a static resource server

Wechat e-book reading of applet completion works (7) Interim inspection report

Wechat e-book reading applet graduation design of applet completion works (1) development outline
随机推荐
Vs Code common shortcut keys
【数据库】期末必知必会-----第七章 数据库完整性
小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
Openresty as a static resource server
PLC OPC 信息模型(DI,PLCopen NodeSets)简介
C语言详解系列——循环语句的练习与巩固,二分查找的讲解
Unity - 如何修改一个 Package 或是如何将 Package Local化
Chapter 2 performance platform godeye source code analysis - data module
XDC 2022 Intel 技术专场:英特尔软硬件技术构筑云计算架构基石
In the era of super video, what is the solution to the data flood?
机器学习11:代价敏感学习
【超能云终端创领先机】本地计算云端管理,英特尔助力教育数字化
In tech 2022 | Intel technology product innovation quick view
Some problems after xcode11 new project
【数据库】期末必知必会-----第十章 数据库编程
Wechat online education video on demand learning applet graduation design (3) background function
Insert the laptop into the headset and still play it out (the personal test is valid)
sql界面切换不能获取焦点
Common methods of C string
Chapter 0 performance platform godeye source code analysis - Course Introduction