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, 逆向思考
1 public class Solution 2 { 3 public int minPathSum(int[][] grid) 4 { 5 int rowSize = grid.length; 6 int colSize = grid[0].length; 7 8 9 for(int i=0; i<rowSize; i++) // row 10 { 11 for(int j=0; j<colSize; j++) // column 12 { 13 // if this point has both left and top 14 if(j-1 >=0 && i-1 >=0) 15 grid[i][j] += Math.min(grid[i][j-1], grid[i-1][j]); 16 else if(j-1 >=0) // if this point only has left 17 grid[i][j] += grid[i][j-1]; 18 else if(i-1 >=0) // if this point only has top 19 grid[i][j] += grid[i-1][j]; 20 // else, this point has no left and no top 21 22 } 23 } 24 25 return grid[rowSize-1][colSize-1]; 26 } 27 }
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
转载于:https://www.cnblogs.com/jimmycheng/p/7223470.html
LeetCode 64. Minimum Path Sum(最小和的路径)相关推荐
- 【DP】LeetCode 64. Minimum Path Sum
LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...
- 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 ...
- 64. Minimum Path Sum 最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [[1,3,1],[1,5,1],[4, ...
- 63. Unique Paths II and 64. Minimum Path Sum
文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...
- 【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 ...
- python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径
1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...
- 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: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:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
最新文章
- Mac 升级系统 pod 命令无效
- Kubernetes 1.14 版本发布:正式支持Windows 节点,持久化本地卷进入GA
- R语言使用scales包的hue_pal函数获取ggplot2任何级别的离散色码、使用scales包的hue_pal函数获取ggplot2任何级别的反序(reverse)离散色码
- Ant Design使用方法
- android 使用compareTo比较大小
- 堆化 二叉堆一般用数组来表示。typedef struct _minHeapNodetypedef struct _otherInfo-icoding-C-数据结构
- 软件工程——理论、方法与实践 第一章
- 在线检测PR值,外链,批量
- [PyTorch] jit.script 与 jit.trace
- 【Codeforces 486C】Palindrome Transformation
- 找出数组中从未出现的最小正整数java实现
- 中标麒麟(linux)下Qt调用python数据转换
- JAVA数据类型转换大全
- xmind转excel脚本(简化版)
- 国内外计算机视觉领域优秀研究团队汇总
- 世界杯的科学---足球的基本原理
- windows文件权限管理dos命令
- 愿天下有情人都是失散多年的兄妹 (25 分)
- 虚拟机内下载速度慢解决方法汇总
- java put 语句_Java put语句
热门文章
- 以下用于数据存储领域的python第三方库是-Python数据存储及表示
- python编程有哪些-Python编程编译器有哪些比较好用
- python工程师月薪-在三线城市,Python工程师也能拿到月薪20K?
- python编程100例头条-我用Python编程语言做了一些神奇好玩的事情
- python开发工程师面试题-2019超实用Python开发工程师面试题分享
- python导入xlsx文件-python怎么用pd导入xlsx
- python条件语句-Python3 条件控制
- python中文什么意思-python是什么
- python编写爬虫的步骤-python网络爬虫(二)编写第一个爬虫
- python基础代码-Python基础(代码)