【leetcode】Minimum Path Sum
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.
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相关推荐
- 【DP】LeetCode 64. Minimum Path Sum
LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...
- 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题
[LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 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 ...
- 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 ...
- 【LeetCode】1. Two Sum
传送门:https://leetcode.com/problems/two-sum/#/description 一.题目描述 Given an array of integers, return in ...
- 【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 ...
- LeetCode 1.Minimum Path Sum 2.Unique Paths I and II
大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...
最新文章
- [Android开发常见问题-12] Android开发中debug.keystore如何使用。
- 如何在 GitHub 上高效搜索开源项目
- dhcp request汉字乱码分析
- 笔记-信息系统开发基础-面向对象基本概念-多态
- GAN生成对抗网络-CGAN原理与基本实现-条件生成对抗网络04
- 【和我一起学习Unity3D】Unity3D的坐标控制
- 时序分析:串匹配—Brute-Force算法
- 前端学习(2879):实现v-if和v-show设计分析 视图搭建
- 想学编程一定要看的文章,你真的了解编程吗?
- UVA - 207 PGA Tour Prize Money
- Shiro——从零开始进行详解官方入门案例
- Google Volley框架源码走读
- 如何关闭父窗体?C#(已解决)
- 100项PPT制作技术
- axios get请求中文乱码
- java类中serialVersionUID的作用
- 一个基于UDP数据广播的局域网络会议程序
- QQ 居然被盗了?原因在这......
- 计量芯片应用心得之软件篇
- 你真的了解什么是Beta测试?完整测试指南