64. Minimum Path Sum 动态规划
description:
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:
Example:
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.
answer:
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if (grid.empty() || grid[0].empty()) return 0;
int m = grid.size(), n = grid[0].size();
vector<vector<int>> dp(m, vector<int>(n));
dp[0][0] = grid[0][0];
for (int i = 1; i < m; ++i) dp[i][0] = grid[i][0] + dp[i - 1][0]; // 边界
for (int j = 1; j < n; ++j) dp[0][j] = grid[0][j] + dp[0][j - 1]; // 边界
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]); // 更新条件
}
}
return dp[m - 1][n - 1];
}
};
relative point get√:
hint :
动态规划,边界情况,更新条件
64. Minimum Path Sum 动态规划的更多相关文章
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- 【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 ...
- 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 ...
- 64. Minimum Path Sum(中等, 又做出一个DP题, 你们非问我开不开心,当然开心喽!^^)
Given an m x n grid filled with nonnegative numbers, find a path from top left to bottom right which ...
随机推荐
- Spring的基础配置,以及注解
常用依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webm ...
- Ubuntu 16.04搭建php5.6 Web服务器环境
Ubuntu 16.04默认安装php7.0环境,但是php7目前兼容性并不是很好,如果自行安装php5需要清除php7的已安装包,否则会报错. 移除默认及已安装的PHP包 sudo dpkg -l ...
- Archlinux zsh终端美化 powerlevel10k配置
环境 Arch Linux + KDE Plasma 安装zsh yay -S zsh 更改默认终端 chsh -s /bin/zsh 安装oh-my-zsh-git archlinuxcn源有打好的 ...
- GO语言的JSON01---序列化
package main import ( "encoding/json" "fmt" ) /* 定义待序列化结构体 属性一定要可见,否则json包无法访问 * ...
- mybatis-plus 分页查询+ dao层抽象
1.配置文件添加paginationInterceptor @Configuration @MapperScan("fama.cost.*.mapper") public clas ...
- L3级自动驾驶
L3级自动驾驶 2020年开年 3月9日,工信部在其官网公示了<汽车驾驶自动化分级>推荐性国家标准报批稿,并拟于2021年1月1日开始实施. 按照中国自身标准制定的自动驾驶分级标准,在千呼 ...
- Salesforce LWC学习(三十四) 如何更改标准组件的相关属性信息
本篇参考: https://www.cnblogs.com/zero-zyq/p/14548676.html https://www.lightningdesignsystem.com/platfor ...
- .Net Redis实战指南——常用命令
本问主要介绍rabbitmqctl工具的常用命令. vhost 一个RabbitMQ服务器可以创建多个虚拟的消息服务器,称之为虚拟主机(virtual host),简称为vhost.vhost之间是绝 ...
- v-for和v-if不能同时使用
如果使用v-for遍历数据时,想筛选出URL不为空的项并进行渲染 <ul> <li v-for="(item,index) in list" v-if=" ...
- 解决git冲突
多个开发者同时操作git中的同一个文件,第一个人在commit和push的时候是可以正常提交的,而之后的开发者执行pull,就会报冲突异常conflict. 解决方案: 全部采用当前更改 之后再去gi ...