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.

int minPathSum(vector<vector<int> > &grid) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row = grid.size();
int col = grid[0].size(); int**dp = new int*[row];
for(int i=0; i< row; i++)
dp[i] = new int[col]; dp[0][0] = grid[0][0];
for(int j =1; j < col; j++)
dp[0][j] = dp[0][j-1] + grid[0][j]; for(int i = 1; i < row; i++)
for(int j =0; j < col; j++)
if(j == 0)dp[i][0] = dp[i-1][0] + grid[i][0];
else{
int tmp = dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
dp[i][j] = tmp + grid[i][j];
} int tmp = dp[row-1][col-1];
for(int i = 0; i < row; i++)
delete[] dp[i];
delete[] dp;
return tmp;
}

leetcode_question_64 Minimum Path Sum的更多相关文章

  1. 【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 b ...

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

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

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

  4. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

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

  6. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

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

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

随机推荐

  1. WebApi个人理解概要

    WebApi概要 Global文件的作用: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class MvcApplication : System.We ...

  2. zen-coding for notepad++,前端最佳手写代码编辑器

    zen-Coding是一款快速编写HTML,CSS(或其他格式化语言)代码的编辑器插件,这个插件可以用缩写方式完成大量重复的编码工作,是web前端从业者的利器. zen-Coding插件支持多种编辑器 ...

  3. C#第三方zip解压压缩工具,带事例源码

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using ICSharpCode. ...

  4. 【MFC相关】MFC入门相关

    1.MFC的“匈牙利标识符命名法”,这是一个约定,可以增加代码的可读性: 声明或定义了一个类,那么这个类可以以“C”(class)为前缀,如CHelloWorldDlg类: 定义一个无符号型的局部变量 ...

  5. 手动添加删除windows服务

    1.使用sc命令创建服务 命令格式如: sc create [service name] [binPath= ] <option1> <option2>... 比如: sc c ...

  6. removeCss

    (function ($) { //删除元素行内style中单个style和多个style //示例: //$(".b").removeCss("color") ...

  7. PhoneGap笔记-01 基本使用

    1. 环境配置 1.1 常用框架 jQuery Backbone.js dojo bootstrap kendo UI Sencha jQuery Mobile PhoneJS AngularJS I ...

  8. Qt 继承QWidget setstylesheet解决

    void myMainWidget::paintEvent(QPaintEvent * e) { QStyleOption opt; opt.init(this); QPainter p(this); ...

  9. 让Apache支持中文Directory的最简方法

    解决方法很简单,一句话,将httpd.conf配置文件的字符编码转换成UTF-8即可. 转换方法也很简单,在记事本中选择 文件->另存为,弹出的窗口中选择编码为UTF-8即可. 重新启动下apa ...

  10. C++访问权限的问题

    以前一直认为对于类中的private数据成员,只有调用该方法的对象才能更能访问自身的私有成员,其他的类在该成员函数(公共接口)中也无法调用自身的私有成员,今天看到<c++ prime plus& ...