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. ODBC、OLEDB和ADO之间的关系 ,以及性能比较

    学习了.net视频之后,对里面涉及到的数据库连接部分中的一些概念表示很无语.网上很多相关资料,但除了网站不一样外,基本上内容都神一样的一致. 现在,我就通过结合看到的一些资料再加上自己的理解试图去解释 ...

  2. Django框架(十):视图(三) Cookie、Session

    1. Cookie Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密).Cookie最早是网景公司的前雇员L ...

  3. request.getParameter()获取不到数据的问题

    最近做项目时,发现手机客户端通过http协议post方式上传数据到服务端,在服务器端通过request.getInputStream()能获取到相应的数据,但用request.getParameter ...

  4. 同时运行两个版本相同的tomcat

    由于项目需要,代理集群和一个节点都部署在本地,那么就需要有两个tomcat,一个部署集群,一个部署项目,我都用了7.0.34版本的tomcat 当启动代理的tomcat成功时,再启动节点的tomcat ...

  5. miniconda安装jupyter

    1.安装jupyter 由于miniconda是anaconda的简化版,只有一个prompt: 安装jupyter,只需要打开prompt的dos窗口,输入命令pip install jupyter ...

  6. JVM学习思维导图

  7. IntelliJ IDEA2018.2.7安装和破解教程

    一.安装 IntelliJ IDEA2018.2.7 IDEA官网下载地址链接:https://www.jetbrains.com/idea/download/previous.html 1.进入网站 ...

  8. servlet 3 通过编程的方式来配置ServletContext

    你是否再为配置文件web.xml容易出错而烦恼?是否为web.xml文件存放位置而不知所措?是否为web.xml为什么要这样配?怎么才能更好的配置web.xml而烦恼?那么一种新的方式出现了: spr ...

  9. 爬虫加入数据post请求

    formdata = {'...': '...', '......': '......', '......': '......'}HEADERS = { 'User-Agent': 'Mozilla/ ...

  10. mysql创建视图和存储过程,变量

    创建视图 sql>create view 视图名 as select语句; 修改视图并添加别名 sql>create or replace view empvu10 (employee_n ...