首页 > 其他分享 >64. Minimum Path Sum

64. Minimum Path Sum

时间:2022-11-13 15:58:41浏览次数:52  
标签:Given int Sum else Minimum grid && path Path

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 1:

[[1,3,1], 
 [1,5,1], 
 [4,2,1]]
Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.   public int minPathSum(int[][] grid) {        int m = grid.length;        int n = grid[0].length;        for (int i = 0; i < m; i++) {            for (int j = 0; j < n; j++) {               if (i == 0 && j != 0)                  grid[i][j] = grid[i][j] + grid[i][j - 1]; //对于第一行               else if (i != 0 && j == 0)                  grid[i][j] = grid[i][j] + grid[i - 1][j]; //对于第一列               else if (i == 0 && j == 0)                  grid[i][j] = grid[i][j];               else                  grid[i][j] += Math.min(grid[i][j - 1], grid[i - 1][j]); //看此格左和上哪个更小                           }        }      return grid[m - 1][n - 1];       }

标签:Given,int,Sum,else,Minimum,grid,&&,path,Path
From: https://www.cnblogs.com/MarkLeeBYR/p/16886078.html

相关文章

  • 62. Unique Paths
    Thereisarobotonan mxn grid.Therobotisinitiallylocatedatthe top-leftcorner (i.e., grid[0][0]).Therobottriestomovetothe bottom-right......
  • Specific static library / shared library / header path on OSX
    StaticlibrarypathexportLIBRARY_PATH="/usr/local/lib"SharedlibraryordynamiclibrarypathexportDYLD_LIBRARY_PATH="/usr/local/lib"Headerfilepathex......
  • kafka-consumer-groups 命令行工具使用手册,Kafka 管理必备
    kafka-consumer-groups命令行工具使用手册该手册原文出自​​$KAFKA_HOME\bin\windows\kafka-consumer-groups.bat--help​​命令的输出结果,并由​​Redisant​​提供......
  • 2.搭建服务消费者user-consumer
    搭建服务消费者user-consumer1.创建user-consumer模块,导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spri......
  • 基于CSS mask和clip-path实现切角的技巧
    本文翻译自TrickstoCutCornersUsingCSSMaskandClip-PathProperties,略有删改原作者:TemaniAfif我们最近使用CSSmask属性创建花哨的边框,本文将使用CSSmask和c......
  • 1007 Maximum Subsequence Sum
    题目:1007MaximumSubsequenceSumGivenasequenceof K integers{ N1​, N2​,..., NK​ }.Acontinuoussubsequenceisdefinedtobe{ Ni​, Ni+1​,........
  • SpringMVC-解析@PathVariable
    DispatcherServlet.doService去处理请求时,调用getHandler去获取匹配请求的handle。会调用RequestMappingInfoHandlerMapping.handleMatch,这个方法会调用extractMatchDetai......
  • [ABC271E] Subsequence Path
    洛谷链接原题链接题目描述某地区有\(N\)个城镇,编号为1到\(N\),并且由\(M\)条公路连接,编号1到\(M\)。每条公路都是有向的;而且编号为\(i(1\lei\leM)\)......
  • Pythin - pathlib
    简介跨平台,python内置PurePath:处理路径字符串Path:处理文件系统的真实路径获取功能#将当前文件构建为Path对象path_obj=Path(__file__)print(f'path_obj=......
  • 一文搞懂Path环境变量
    什么是Path环境变量?在探讨这个问题之前,我们需要了解什么是环境变量。“环境变量”和“path环境变量”其实是两个东西,这一点大家一定要区分开,不要混为一谈。“环境变量”......