[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.
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
题意:
给定一个二维矩阵, 找出一条从左上角到右下角的path,能使得这条path经过的所有数字相加之和最小
思路:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
二维dp
1 4 5 2 ? dp[i][j] 6
初始化,
dp[0][0] = grid[0][0]
是否需要预处理第一个row: dp[0][j], 因为矩阵中间的dp[i][j]既可能来自上方,也可能来自左方。所以先预处理仅来自左方的path数字之和 dp[0][j] = dp[0][j-1] + grid[0][j]
是否需要预处理第一个col:dp[i][0],因为矩阵中间的dp[i][j]既可能来自上方,也可能来自左方。 所以先预处理仅来自上方的path数字之和 dp[i][0] = dp[i-1][0] + grid[i][0]
转移方程,
因为矩阵中间的dp[i][j]既可能来自上方,也可能来自左方, 要使得path的数字之和最小,必须比较上方和左方的结果哪个更小,再相加到当前的grid[i][j]上
dp[i][j] = min( dp[i-1][j], dp[j-1][i] ) + grid[i][j]
代码:
class Solution {
public int minPathSum(int[][] grid) {
// init
int[][] dp = new int[grid.length ][ grid[0].length];
dp[0][0] = grid[0][0];
for(int i = 1; i< grid.length; i++){
dp[i][0] = grid[i][0] + dp[i-1][0] ;
}
for(int j = 1; j< grid[0].length; j++){
dp[0][j] = grid[0][j] + dp[0][j-1] ;
}
// func
for(int i = 1; i< grid.length; i++){
for(int j = 1; j< grid[0].length; j++){
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
}
}
return dp[grid.length-1 ][ grid[0].length-1];
}
}
[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] 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(最小走棋盘 动态规划)
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 ...
- 064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...
- 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 ...
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
随机推荐
- 全志A33 lichee Linux内核原子操作(附实测代码)
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 原子操作是指不会被线程调度机 ...
- xgboost实例代码
# -*- coding: utf-8 -*- import xgboost as xgb import csv import jieba jieba.load_userdict('wordDict. ...
- ubuntu18.04安装openresty
ubuntu18.04使用openresty官方APT源安装openresty 添加openresty的 APT 仓库,这样就可以便于未来安装或更新软件包(通过 apt-get update 命令). ...
- MySQL 烂笔头 备份和还原
备份 mysqldump -u root -p testdb > d:/backupfile.sql 还原 mysql -u root -p testdb2 <d:/backupfile. ...
- 黄聪:windows下使用xampp3.2.2配置多个监听端口和不同的网站目录
windows下使用xampp3.2.2配置多个监听端口和不同的网站目录 一:配置Apache文件httpd.conf 打开Apache的配置文件httpd.conf,可以通过点击xampp的Apac ...
- js鼠标拖动(转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python【每日一问】03
问:请给出下列代码的执行结果,并解释 a = dict.fromkeys([6, 7, 8], ["testing", {"name": "ken&q ...
- Excel宏录制、数据透视表、合并多个页签
前段时间做数据分析的时候,遇到很多报表文件需要处理,在此期间学习了很多Excel操作,特此做笔记回顾. Excel宏录制 打开开发者工具 打开Excel文件,选择”文件”-->“选项”--> ...
- CentOS安装mysql源码包
1.# cd /usr/local/src 2.上传mysql.tar.gz文件 3.# tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 4.# ...
- React开发笔记
项目环境搭建 使用create-react-app CSS使用styled-components yarn add styled-components 引入reset.css样式 import { c ...