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.

You can only move either down or right at any point in time.

 public class Solution {
/**
* @param grid: a list of lists of integers.
* @return: An integer, minimizes the sum of all numbers along its path
*/
public int minPathSum(int[][] grid) {
if (grid == null) return ; // initialize the first row
int [][] min = new int[grid.length][grid[].length]; min[][] = grid[][]; for (int i = ; i < grid[].length; i++) {
min[][i] = min[][i - ] + grid[][i];
} for (int i = ; i < grid.length; i++) {
min[i][] = min[i - ][] + grid[i][];
} for (int i = ; i < grid.length; i++) {
for (int j = ; j < grid[].length; j++) {
min[i][j] = (int)Math.min(min[i - ][j], min[i][j - ]) + grid[i][j];
}
} return min[grid.length - ][grid[].length - ];
}
}

Minimum Path Sum的更多相关文章

  1. 【leetcode】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 ...

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

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

  3. 【LeetCode练习题】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之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  5. LeetCode: 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 ...

  6. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

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

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

  10. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

随机推荐

  1. abstract和接口

    接口只包含常量和抽象方法,不能实例化. abstract: 1.抽象类不能实例化, 2.可以没有抽象方法.但有了抽象方法,一定要被定义为抽象类. 3.子类没有实现父类中所有的抽象方法.子类也必须定义为 ...

  2. sql-and、or

    WHERE 指令可以被用来由表格中有条件地选取资料. 这个条件可能是简单的 (像上一页的例子),也可能是复杂的.复杂条件是由二或多个简单条件透过 AND 或是 OR 的连接而成.一个 SQL 语句中可 ...

  3. mac os x常用快捷键及用法

    最近在研究mac os x系统,开始入手,很不习惯,和windows差别很大,毕竟unix内核.使用中总结了一些使用快捷键(默认),持续更新,欢迎大家补充.1.撤销:command+z 保存:comm ...

  4. python 学习5--matplotlib画图实践

    ### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 学习参考: http://www.cnblogs.com/vamei/archive/2013/01/30/28 ...

  5. js阻止表单重复提交

    //校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); a ...

  6. PL/0编译器(java version)–Pcode.java

    1: package compiler; 2:   3: /** 4: * //虚拟机指令 5: * 6: * @author jiangnan 7: * 8: */ 9: public class ...

  7. Spring学习5-Spring整合JDBC及其事务处理(注解方式)

    一.整合的步骤   1.步骤一:首先要获得DataSource连接池(推荐使用B方式): 要对数据库执行任何的JDBC操作,需要有一个Connection.在Spring中,Connection对象是 ...

  8. js中按钮控制显示隐藏以及下拉功能

    <script> function show() { var a2=document.getElementById("div2"); if(a2.style.displ ...

  9. 锋利的jQuery-4--动画方法总结简表

  10. 新浪微博客户端(8)-添加按钮到TabBar

    在tabBar的中部添加一个发送微博的按钮. DJTabBar.h #import <UIKit/UIKit.h> @class DJTabBar; @protocol DJTabBarD ...