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.
class Solution {
public int minPathSum(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
int[][] paths = new int[row][col];
paths[0][0] = grid[0][0];
// start from 1 should include the current grid value and prev value
for (int i = 1; i < row; i++) {
paths[i][0] = grid[i][0] + paths[i - 1][0];
}
for (int i = 1; i < col; i++) {
paths[0][i] = grid[0][i] + paths[0][i - 1];
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < col; j++) {
paths[i][j] = Math.min(paths[i - 1][j], paths[i][j - 1]) + grid[i][j];
}
}
return paths[row - 1][col - 1];
}
}

[LC] 64. Minimum Path Sum的更多相关文章

  1. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  2. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  3. 【LeetCode】64. 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 ...

  4. [LeetCode] 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 ...

  5. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

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

  7. C#解leetcode 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 ...

  8. LeetCode OJ 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 ...

  9. LeetCode 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 ...

随机推荐

  1. SEO初步学习之影响网站排名的因素

    本文介绍一些比较明显的因素,一些隐藏较深的原因还有待发掘: 1.采集网站内容,即抄袭其他网站的内容. 2.新站上传后建议不要有大的改动. 3.标题频繁修改. 4.大量投放垃圾外链. 5.不做友链,交友 ...

  2. js中要声明变量吗?

    你好,js语言是弱类型语言,无需申明即可直接使用,默认是作为全局变量使用的.建议:在function里时应使用var 申明变量,这样改变量仅仅只在function的生存周期内存在,不会污染到,全局控件 ...

  3. 将元素平分成差值最小的两个集合(DP)

    现有若干物品,要分成较为平均的两部分,分的规则是这样的: 1)两部分物品的个数最多只能差一个. 2)每部分物品的权值总和必须要尽可能接近. 现在请你编写一个程序,给定现在有的物品的个数以及每个物品的权 ...

  4. layui select恢复默认值

  5. python格式化输出的三种形式

    法一: list_a = [1, 2, 3] str_b = 'aaa' string = "There are two contents:%s, %s" % (list_a, s ...

  6. c#学习笔记01——引用&类

    数据类型 值类型 bool 布尔值 True 或 False False byte 8 位无符号整数 0 到 255 0 char 16 位 Unicode 字符 U +0000 到 U +ffff ...

  7. 2019CSP-J游记

    2019-10-19:开一个坑,今天初赛,我是我们考场唯一几个坚持到16:45收卷的人,我们是机试,竟然可以用编译器. 这次初赛总体感觉打得不错,卷面满分200,最后实际分数,就是卷面分除以二. 初赛 ...

  8. 《Premiere Pro 2020》初心版_v3 14.0.1.71

    <Premiere Pro 2020>初心版_v3 下载地址(78e7) SHA1:8B081196C1756CE9477A0D056D6331907B3DDFDC 版本信息    发行版 ...

  9. Django框架(五):模型(一) 定义属性

    1. 定义属性 Django根据属性的类型确定以下信息: 当前选择的数据库支持字段的类型 渲染管理表单时使用的默认html控件 在管理站点最低限度的验证 django会为表创建自动增长的主键列,每个模 ...

  10. OpenCV On Android环境配置最新&最全指南(Android Studio篇)

    本文是从本人简书上搬运而来,属本人原创,如有转载,请注明出处:http://www.jianshu.com/p/6e16c0429044 简介 本文是<OpenCV On Android环境配置 ...