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 minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
题目标签:Array
这道题目和前面两题差不多,基本思想都是一样的。这题要求我们找到最小和的路径。同样用Dynamic programming,我们可以分析一下,path 只能走右边和下面,那么对于每一个点,它的path 只可能从左边和上面来,我们只要在两者中取一个小的加上自己就可以了。因此,遍历gird,对于每一个点,有四种情况:
case 1:它有left 和top,取小的加上自己;
case 2:它只有left,left + 自己;
case 3:它只有top, top + 自己;
case 4:它没有left 和top,什么都不需要做。
最后返回终点的值就可以了。这里可以in-place, 也可以自己新建立一个matrix。
Java Solution:
Runtime beats 35.80%
完成日期:07/22/2017
关键词:Array
关键点:Dynamic Programming, 逆向思考
public class Solution
{
public int minPathSum(int[][] grid)
{
int rowSize = grid.length;
int colSize = grid[0].length; for(int i=0; i<rowSize; i++) // row
{
for(int j=0; j<colSize; j++) // column
{
// if this point has both left and top
if(j-1 >=0 && i-1 >=0)
grid[i][j] += Math.min(grid[i][j-1], grid[i-1][j]);
else if(j-1 >=0) // if this point only has left
grid[i][j] += grid[i][j-1];
else if(i-1 >=0) // if this point only has top
grid[i][j] += grid[i-1][j];
// else, this point has no left and no top }
} return grid[rowSize-1][colSize-1];
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 64. Minimum Path Sum(最小和的路径)的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [leetcode] 64. Minimum Path Sum (medium)
原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
随机推荐
- 笔记3 linux 多线程 条件变量+互斥锁
//cond lock #include<stdio.h> #include<unistd.h> #include<pthread.h> struct test { ...
- java通过JDBC链接SQLServer2012【转载!!!超详细】
http://blog.csdn.net/stewen_001/article/details/19553173/
- Servlet第五篇【介绍会话技术、Cookie的API、详解、应用】
什么是会话技术 基本概念: 指用户开一个浏览器,访问一个网站,只要不关闭该浏览器,不管该用户点击多少个超链接,访问多少资源,直到用户关闭浏览器,整个这个过程我们称为一次会话. 为什么我们要使用会话技术 ...
- temp-黄河农商行路径
-------------------------------黄河农村商业银行------------------------------------ --1.--svn 地址:http://10.0 ...
- Java对象克隆详解
原文:http://www.cnblogs.com/Qian123/p/5710533.html 假如说你想复制一个简单变量.很简单: int apples = 5; int pears = appl ...
- Azure SQL Database (25) Azure SQL Database创建只读用户
<Windows Azure Platform 系列文章目录> 本文将介绍如何在Azure SQL Database创建只读用户. 请先按照笔者之前的文章:Azure SQL Databa ...
- 【京东详情页】——原生js爬坑之标签页
一.引言 要做详情页的商品评价等5个li的标签页转换,效果如下: 二.实现原理 有一个特别的地方:上面五个li,但下面只有四个容器(table/div). 设计的目的:无论点哪个li,只有前四个div ...
- 云计算之openstack ocata 项目搭建详细方法
之前写过一篇<openstack mitaka 配置详解>然而最近使用发现阿里不再提供m版本的源,所以最近又开始学习ocata版本,并进行总结,写下如下文档 OpenStack ocata ...
- JAVA多线程---ThreadLocal<E>
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px ".SF NS Text" } tips: 1 当前ThreadLocal ...
- mybatis 错误CGLIB is not available
### Error querying database. Cause: java.lang.IllegalStateException: Cannot enable lazy loading beca ...