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 动态规划的更多相关文章

  1. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  2. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  3. 【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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 由Chromium内核引起的微信内置浏览器rce漏洞复现

    背景 chrome浏览器爆出漏洞,github上公开了poc:https://github.com/r4j0x00/exploits/tree/master/chrome-0day,在关闭chrome ...

  2. 在react中使用redux并实现计数器案例

    React + Redux 在recat中不使用redux 时遇到的问题 在react中组件通信的数据是单向的,顶层组件可以通过props属性向下层组件传递数据,而下层组件不能向上层组件传递数据,要实 ...

  3. 【七】Kubernetes 探针介绍 - 存活、就绪探针案例测试

    一.探针概述 探针是有 kubelet 对容器执行的定期诊断,并不是由 Master 节点发起的探测,而是由每一个 Node 所在的 kubelet 进行探测,这样可以减轻 Master 节点系统负载 ...

  4. modelMapper使用,将数据库查询对象直接转成DTO对象

    1.pom引入 <dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmap ...

  5. 前端工具 | JS编译器 Brace 使用教程

    前言 开发人员一般是在电脑上面安装了IDE完成日常的开发任务,因为项目业务需求,用户想要在线写JS脚本,纯粹的字符串,很"费用户".那就需要一个在线JS编译器,需要轻量级,好用,语 ...

  6. 超轻量AI引擎MindSpore Lite

    超轻量AI引擎MindSpore Lite 揭秘一下端上的AI引擎:MindSpore Lite. MindSpore Lite是MindSpore全场景AI框架的端侧引擎,目前MindSpore L ...

  7. NVIDIA Tensor Cores解析

    NVIDIA Tensor Cores解析 高性能计算机和人工智能前所未有的加速 Tensor Cores支持混合精度计算,动态调整计算以加快吞吐量,同时保持精度.最新一代将这些加速功能扩展到各种工作 ...

  8. 使用adb命令在模拟器安装apk

    1.adb connect 127.0.0.1:7555 连接网易模拟器地址2.adb devices -L 查看有几个连接端口地址3.执行推送配置文件 命令 adb push 文件 路径

  9. Spring——Bean的作用域

    Spring中Bean的作用域有五种,分别是singleton.prototype.request.session.globalSession.其中request.session.globalSess ...

  10. 【NX二次开发】Block UI 选择单元

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...