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. VUE,index key v-for

    列表渲染语法  v-forv-for 循环对象 <article v-for="(item, key, index) of info">{{item}} {{key}} ...

  2. awk使用笔记

    awk特殊字符打印方法: 1.awk打印双引号:    awk '{print "\""}' 2.awk打印单引号: awk '{print "'\''&quo ...

  3. 1. react 基础 简介 及 环境搭建

    一.简介 由 Facebook 推出 2013 年 开源 的 函数式编程的 使用人数最多的 前端框架 拥有健全的文档与完善的社区 ( 官网 ) react 16 称为 React Fiber ( 底层 ...

  4. keil5最新破解教程(可以使用到2032年哦!):

    keil5最新破解教程(可以使用到2032年哦!): 首先附上破解软件下载链接:https://github.com/lzfyh2017/keil5- 相信不少小伙伴使用的keil5都快要到期了,那么 ...

  5. Django框架(九):视图(二) HttpRequest对象、HttpResponse对象

    1. HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpR ...

  6. [Algo] 131. Deep Copy Linked List With Random Pointer

    Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...

  7. selector的使用,android:clickable="true"

    <ImageView android:id="@+id/patrol_buzzer_btn" android:layout_width="80dp" an ...

  8. views层回顾

    目录 views层回顾 jsonResponse 2 大文件上传 3. cbv和fbv源码分析 4settings.py源码分析 5模板传值{{}} {%%} 6. 过滤器和标签和自定义 7模板的继承 ...

  9. 如何选字体(font-family)

    一.默认字体情况 1.Window下: 宋体(SimSun):Win下大部分游览器的默认字体,宋体在小字号下(如12px.14px)的显示效果还可以接受,但是字号一大就非常糟糕了,所以使用的时候要注意 ...

  10. 利用CSS制作背景变色的横向导航栏

    1.表单 页面如下: <html> <head> <title>注册表单页面</title> </head> <body> &l ...