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 ...
随机推荐
- 常见判断错误 (Day_30)
写给自己的话: 这是一个卡了我小半天的BUG,也是一个很低端的BUG,写篇博客吧,以后回来看看,会发现曾经的自己是如何的菜. 同样,以此记录我的进步 步入正题,这是我实现多条件分页时遇到的一个BUG, ...
- Win10 安装 Python3 (上)
Python3 For Windows 10 installer 参考 The full installer 安装 随后可以看到,installer 在用户环境变量PATH中,添加了三项: 卸载 使用 ...
- JAVA并发(4)-并发队列ConcurrentLinkedQueue
本文开始介绍并发队列,为后面介绍线程池打下基础.并发队列莫非也是出队.入队操作,还有一个比较重要的点就是如何保证其线程安全性,有些并发队列保证线程安全是通过lock,有些是通过CAS. 我们从Conc ...
- 游刃于私有网络与公共网络之间的NAT
网络地址转化技术NAT 1. 应用场景 2. NAT 2.1 静态NAT 2.2 动态NAT 2.3 NAPT 2.4 EASY IP 3. NAT配置 3.1 静态NAT 3.2 动态NAT 3.3 ...
- js的基本数据类型和typeof的关系
JavaScript数据类型是非常简洁的,它定义了6中基本数据类型 null:空.无.表示不存在,当为对象的属性赋值为null,表示删除该属性 undefined:未定义.当声明变量却没有赋值时会显示 ...
- CUDA 7 流并发性优化
异构计算是指高效地使用系统中的所有处理器,包括 CPU 和 GPU .为此,应用程序必须在多个处理器上并发执行函数. CUDA 应用程序通过在 streams 中执行异步命令来管理并发性,这些命令是按 ...
- pytest的allure的环境配置
一.下载地址: https://github.com/allure-framework/allure2/releases 二.配置环境变量: 三.验证allure安装成功
- Selenium-python 之弹窗处理
在Selenium-python 中,有时候需要对弹窗进行处理,比如获取弹窗上的内容.确定.取消.在弹窗上输入内容后点击确定-再次点出弹窗,需要专门的处理. 一.获取弹窗的内容 driver.find ...
- 性能工具之Jmeter-Dubbo脚本开发
内容目录: 1.idea 环境项目部署 2.nacos 环境部署 3.dubbo插件部署 4.不带参数请求 5.带参参数请求 Apache Dubbo 是一款高性能.轻量级的开源Java RPC框架 ...
- 【Linux进阶】使用grep、find、sed以及awk进行文本操作
目录 一.元字符 二.grep命令 1. 过滤出包含某字符串的行 2. 过滤出以某字符串开头(结尾)的行 3. 过滤出包含某字符串及其相邻的行 4. 过滤出不包含某关键字的行 5. 过滤出包含多个字符 ...