64. Minimum Path Sum

2023-05-16

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

Example:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

solution

在这里插入图片描述

class Solution {
   public int minPathSum(int[][] grid) {
        int rows = grid.length;
        int cols = grid[0].length;
        int[][] minValue = new int[rows][cols];

        minValue[0][0] = grid[0][0];
        //这里和求最大值不一样,需要将第一排和第一列单独进行计算
        for(int i = 1; i < rows; i++){
            minValue[i][0] = minValue[i-1][0] + grid[i][0];
        }
        for(int j = 1; j < cols; j++){
            minValue[0][j] = minValue[0][j-1] + grid[0][j];
        }

        for(int i = 1; i < rows; i ++){
            for(int j = 1; j < cols; j++){
                minValue[i][j] = Math.min(minValue[i-1][j] , minValue[i][j-1] ) + grid[i][j];
            }
        }
        return minValue[rows-1][cols-1];
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

64. Minimum Path Sum 的相关文章

随机推荐

  • Android UI深度理解:Activity UI视图结构

    Activity UI视图结构 每个Activity都会获得一个窗口 xff0c 那就是Window xff0c 它用于绘制用户的UI界面 Window是一个抽象类 xff0c 提供了绘制窗口的一组通用API xff0c PhoneWind
  • 学习嵌入式必读十本书,从C语言到ARM

    学习嵌入式必读的十本书籍 xff0c 按照C语言 数据结构 Linux C 43 43 QT 单片机 ARM的顺序给大家推荐 01 C语言 凡是计算机 电子 通信 自动化 机械专业的同学 xff0c 大一的时候必学C语言 xff0c 而且大
  • 研一(研究生)看论文文献必须要知道的几个网站

    找论文看文献必备的几个网站 明确研究方向查找论文看英文文献看文献方法想说的几句话 明确研究方向 刚进入研究生阶段的同学们都干劲十足 xff0c 充满无限期待 但是没有正确的方向很容易让你的努力白费 xff0c xff08 有人会说丰富了自己
  • 【网络教程】群晖自动更新 Docker 映像与容器(Watchtower)【转】

    群晖自动更新 Docker 映像与容器 xff08 Watchtower xff09 更多内容
  • git多账号配置

    什么叫多账号配置 xff0c 也就是说假如你在公司用的gitlab服务器 xff0c 但是自己还有用到GitHub xff0c 那么此时你在本地就需要配置多个ssh key 步骤如下 xff1a 利用ssh keygen t rsa f g
  • Ubuntu 18.04和windows建立共享文件夹

    1 安装samba sudo apt install samba sudo apt install cifs utils 2 创建共享目录 mkdir home yourname share yourname是home下一个文件夹 xff0
  • Linux权限详解,多用户多组各种权限配置原理

    网上有太多关于Linux权限详解 xff0c 这里不啰嗦那些 主要解释下这些权限是杂用的 xff0c 否则知道了什么用户 组之类的权限也配不好 一 权限分类 r xff1a 读取权限 xff0c 数字代号为 34 4 34 w xff1a
  • 1.tow sum

    文章目录 题目c 43 43 版本java版本利用hashmap正确做法 题目 Two Sum Easy Given an array of integers return indices of the two numbers such t
  • 2. Add Two Numbers

    2 Add Two Numbers Medium 59971568Share You are given two non empty linked lists representing two non negative integers T
  • Linux VNC server的安装及简单配置使用

    Linux VNC server的安装及简单配置使用 转 xff1a http www 2cto com os 201309 241104 html Linux VNC server的安装及简单配置使用 摘要 xff1a Linux vnc
  • 3. Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Given a string find the length of the longest substring without repeating
  • 4. Median of Two Sorted Arrays

    Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n respectively Find the media
  • 7. Reverse Integer

    文章目录 Reverse IntegersolutionAlgorithm Reverse Integer Easy Given a 32 bit signed integer reverse digits of an integer Ex
  • 8. String to Integer (atoi)

    String to Integer atoi Implement atoi which converts a string to an integer The function first discards as many whitespa
  • 9. Palindrome Number

    Palindrome Number Easy Determine whether an integer is a palindrome An integer is a palindrome when it reads the same ba
  • 1071. Greatest Common Divisor of Strings

    1071 Greatest Common Divisor of Strings Easy 30985Share For strings S and T we say 34 T divides S 34 if and only if S 61
  • 这个问题搞了我一天

  • 150逆波兰式

    文章目录 150 Evaluate Reverse Polish NotationSolution 150 Evaluate Reverse Polish Notation Medium Evaluate the value of an a
  • 收到礼物最大值

    题目描述 在一个m n的棋盘的每一个格都放有一个礼物 xff0c 每个礼物都有一定价值 xff08 大于0 xff09 从左上角开始拿礼物 xff0c 每次向右或向下移动一格 xff0c 直到右下角结束 给定一个棋盘 xff0c 求拿到礼物
  • 64. Minimum Path Sum

    64 Minimum Path Sum Given a m x n grid filled with non negative numbers find a path from top left to bottom right which