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.

动态规划即可,与Unique Path类似
 1 class Solution {
 2 public:
 3     int minPathSum(vector<vector<int> > &grid) {
 4
 5         int m=grid.size();
 6         int n=grid[0].size();
 7
 8        /* int **dp=new int *[m];
 9         for(int i=0;i<m;i++)
10         {
11             dp[i]=new int[n];
12         }
13         */
14
15         vector<vector<int>> dp(m,vector<int>(n));
16
17         dp[0][0]=grid[0][0];
18
19         for(int i=1;i<m;i++)
20         {
21             dp[i][0]=dp[i-1][0]+grid[i][0];
22         }
23
24         for(int j=1;j<n;j++)
25         {
26             dp[0][j]=dp[0][j-1]+grid[0][j];
27         }
28
29         for(int i=1;i<m;i++)
30         {
31             for(int j=1;j<n;j++)
32             {
33                 dp[i][j]=grid[i][j]+min(dp[i-1][j],dp[i][j-1]);
34             }
35         }
36
37         return dp[m-1][n-1];
38
39     }
40 };

转载于:https://www.cnblogs.com/reachteam/p/4203661.html

【leetcode】Minimum Path Sum相关推荐

  1. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

  2. 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题

    [LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  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

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. 【LeetCode】1. Two Sum

    传送门:https://leetcode.com/problems/two-sum/#/description 一.题目描述 Given an array of integers, return in ...

  8. 【leetcode】1104. Path In Zigzag Labelled Binary Tree

    题目如下: In an infinite binary tree where every node has two children, the nodes are labelled in row or ...

  9. LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

    大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...

最新文章

  1. [Android开发常见问题-12] Android开发中debug.keystore如何使用。
  2. 如何在 GitHub 上高效搜索开源项目
  3. dhcp request汉字乱码分析
  4. 笔记-信息系统开发基础-面向对象基本概念-多态
  5. GAN生成对抗网络-CGAN原理与基本实现-条件生成对抗网络04
  6. 【和我一起学习Unity3D】Unity3D的坐标控制
  7. 时序分析:串匹配—Brute-Force算法
  8. 前端学习(2879):实现v-if和v-show设计分析 视图搭建
  9. 想学编程一定要看的文章,你真的了解编程吗?
  10. UVA - 207 PGA Tour Prize Money
  11. Shiro——从零开始进行详解官方入门案例
  12. Google Volley框架源码走读
  13. 如何关闭父窗体?C#(已解决)
  14. 100项PPT制作技术
  15. axios get请求中文乱码
  16. java类中serialVersionUID的作用
  17. 一个基于UDP数据广播的局域网络会议程序
  18. QQ 居然被盗了?原因在这......
  19. 计量芯片应用心得之软件篇
  20. 你真的了解什么是Beta测试?完整测试指南

热门文章

  1. MySQL数据库表分区功能详解
  2. 使用xshell6连接linux提示 WARNING! The remote SSH server rejected X11 forwarding
  3. python反射机制
  4. android-清单小文件
  5. java的Junit单元测试
  6. CreateProcess 执行CMD命令,并重定向输出
  7. 纪念逝去的岁月——C/C++快速排序
  8. 【转载】Android之用PopupWindow实现弹出菜单
  9. cocos2d-x 如何使用Visual Studio 2010和xcode 4混合编写手机游戏
  10. SQL精选习题及解答