Leetcode: Minimum Path Sum

news/2023/6/10 22:29:27
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.

与Unique Path问题相似,是一个DP问题,解决问题的方案依然是建造一个新矩阵,每个元素值是从top left到该点的最短距离。递归式可以根据这个写出来,并且一旦计算出了到该点的最短距离,就把它存起来以备其他点使用。

递推式是res[i][j]=min(res[i-1][j], res[i][j-1])+grid[i][j]。总共进行两层循环,时间复杂度是O(m*n)。空间复杂度本来应该是O(M*N)的一个矩阵,但是我们实际需要的其实只有上一行的信息,这样可以省去一维,只需要O(m)就够了

注意12行是从左到右

 1 public int minPathSum(int[][] grid) {
 2     if(grid == null || grid.length==0 || grid[0].length==0)
 3         return 0;
 4     int[] res = new int[grid[0].length];
 5     res[0] = grid[0][0];
 6     for(int i=1;i<grid[0].length;i++)
 7     {
 8         res[i] = res[i-1]+grid[0][i];
 9     }
10     for(int i=1;i<grid.length;i++)
11     {
12         for(int j=0;j<grid[0].length;j++)
13         {
14             if(j==0)
15                 res[j] += grid[i][j];
16             else
17                 res[j] = Math.min(res[j-1], res[j])+grid[i][j];
18         }
19     }
20     return res[grid[0].length-1];
21 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/3958961.html


https://dhexx.cn/news/show-17254.html

相关文章

局部权重线性回归(Locally weighted linear regression)

在线性回归中&#xff0c;因为对參数个数选择的问题是在问题求解之前已经确定好的&#xff0c;因此參数的个数不能非常好的确定&#xff0c;假设參数个数过少可能拟合度不好&#xff0c;产生欠拟合(underfitting)问题&#xff0c;或者參数过多&#xff0c;使得函数过于复杂产生…

前台页面优化全攻略(四)

通过前几篇文章&#xff0c;你应该已经掌握了很多优化网站的方法。现在你的网站加载速度已经很快了&#xff0c;但是你必须持续的监控你的网站&#xff0c;了解它的大小变化&#xff0c;要不然一段时间过去之后&#xff0c;它可能又成为了一个胖子。 如今每个页面平均已经达到1…

mysql索引优化 - explain性能分析详细概述

expain出来的信息有10列&#xff0c;分别是id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra 概要描述&#xff1a; id:选择标识符 select_type:表示查询的类型。 table:输出结果集的表 partitions:匹配的分区 type:表示表的连接类型 possible_k…

mysql索引优化 - 单表如何使用索引优化 以及 常见的索引失效的原因分析

1. 全值匹配我最爱&#xff0c;查询的字段按照顺序在索引中都可以匹配到&#xff01; 建立索引 CREATE INDEX idx_age_deptid_name ON emp(age,deptid,NAME); EXPLAIN SELECT SQL_NO_CACHE * FROM emp WHERE emp.age30 EXPLAIN SELECT SQL_NO_CACHE * FROM emp WHERE emp.age…

c#解析Josn(解析多个子集,数据,可解析无限级json)

首先引用 解析类库 using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace BPMS.WEB.Common {public class CommonJsonModel : CommonJsonModelAnalyzer{private string rawjson;private bool isValue false;private bool isModel…

mysql索引优化 - 多表关联查询优化

1 left joinEXPLAIN SELECT * FROM class LEFT JOIN book ON class.card book.card;LEFT JOIN条件用于确定如何从右表搜索行&#xff0c; 左边一定都有&#xff0c; #所以右边是我们的关键点&#xff0c;一定需要建立索引。结论&#xff1a;在优化关联查询时&#xff0c;只有在…

mysql索引优化 - 子查询优化

结论&#xff1a; 在范围判断时&#xff0c;尽量不要使用 not in 和 not exists&#xff0c;使用 left join on xxx is null 代替。 取所有不为掌门人的员工&#xff0c;按年龄分组&#xff01; select age as 年龄, count(*) as 人数 from t_emp where id not in (select ceo…

树莓派折腾---红外探测

先上个图&#xff1a; 用到的配件&#xff1a; 1.主角&#xff1a;树莓派 2.配角&#xff1a;红外探测 3.打杂&#xff1a;面包板&#xff0c;杜邦线&#xff0c;蜂鸣器&#xff0c;LED&#xff0c;电阻 红外探测有三个针脚&#xff0c;两端的是供电&#xff0c;中间是信号输出…

mysql索引优化 - 排序分组优化

where 条件和 on 的判断这些过滤条件&#xff0c;作为优先优化的部分&#xff0c;是要被先考虑的&#xff01; 其次&#xff0c;如果有分组和排序&#xff0c;那么 也要考虑 grouo by 和 order by。1. 必须有过滤&#xff0c;才会用到索引 结论&#xff1a;where&#xff0c;li…

UIView详解

来源&#xff1a;http://blog.csdn.net/chengyingzhilian/article/details/7894276 UIView表示屏幕上的一块矩形区域&#xff0c;它在App中占有绝对重要的地位&#xff0c;因为IOS中几乎所有可视化控件都是UIView的子类。负责渲染区域的内容&#xff0c;并且响应该区域内发生的…