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. tensorflow 损失计算--MSN

    1.tf.losses.mean_squared_error函数 tf.losses.mean_squared_error( labels, predictions, weights=1.0, sco ...

  2. Thread--volatile详细

  3. js中要声明变量吗?

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

  4. SALESGROSSSALES_成本_利润

    //获取成本GETCOST_TMP:NoConcatenateLOAD T_SAL_OUTSTOCK.LE_ID, [T_SAL_OUTSTOCK.LCY CODE], T_SAL_OUTSTOCK. ...

  5. c语言中assert的用法

    /************************************************************************* > File Name: assert.c ...

  6. UML-逻辑架构和包图-概述

    回顾前几章学习了用例模型,本章开始学习设计模型.

  7. Java 开发者必须了解的 16 个Java 顶级开源项目!

    本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb/JavaGuide ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核 ...

  8. win10下挂载efi分区

    管理员身份打开cmd 1.输入diskpart, 2.输入list disk,列出所有的disk 3.select disk xxx,xxx代表你要选的disk 数字,比如:select disk 0 ...

  9. Docker搭建RabbitMQ(阿里云)

    0 环境 系统环境:centos7 服务器:阿里云 1 正文 1 获取安装RabbitMQ https://hub.docker.com/_/rabbitmq 默认rabbitmq镜像是不带web端管 ...

  10. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #随机调整图片的色彩,定义两种顺序. def di ...