[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] ...
随机推荐
- xamarin C# 安卓实现 ListView 放大缩小
翻译自java示例https://raw.githubusercontent.com/Xjasz/AndroidZoomableViewGroup/master/ZoomListView.java u ...
- 在IDEA中实战Git-branch
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...
- Java 的 volatile 修饰符
volatile 修饰符,用于多线程同步 volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值.而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存.这 ...
- 服务容错和Hystrix
一.雪崩效应 在微服务架构中,通常有多个服务层调用,如果某个服务不可用,造成调用的服务也不可用,造成整个系统不可用的情况,叫做雪崩效应 二.Hystrix 放雪崩利器Hystrix,基于Netflix ...
- Property referenced in indexed property path is neither an array nor a List nor a Map
记一次传参请求报错,没有解决 Invalid property 'distributeCars[0][ackStatus]' of bean class [com.api6.plate.prototy ...
- nginx upstream轮询配置
nginx upstream nginx的upstream官方地址为:http://nginx.org/cn/docs/http/ngx_http_upstream_module.html 轮询分为多 ...
- Spring Boot使用单元测试
一.Service层单元测试: 代码如下: package com.dudu.service;import com.dudu.domain.LearnResource;import org.junit ...
- react高阶组件
高阶组件 为了提高组件复用性,在react中就有了HOC(Higher-Order Component)的概念.所谓的高阶组件,其本质依旧是组件,只是它返回另外一个组件,产生新的组件可以对属性进行包装 ...
- redis5.0.4 集群搭建
准备工作用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环境. redis 采用 redis-5.0.4 版本. 两台虚拟机都是 CentOS ,一台 CentOS ...
- cv2的安装
第一种 ,直接尝试 pip install cv2 ,大可能报错. 第二种,pip install opencv-python ,大概率 直接成功. 第三种 ,去网上下包 放到 sit_package ...