题目:

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.

代码:

class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if ( grid.empty() ) return ;
const int m = grid.size();
const int n = grid[].size();
vector<int> dp(n, INT_MAX);
dp[] = ;
for ( int i=; i<m; ++i )
{
dp[] += grid[i][];
for ( int j=; j<n; ++j )
{
dp[j] = grid[i][j] + std::min(dp[j-], dp[j]);
}
}
return dp[n-];
}
};

tips:

典型的“DP+滚动数组”,时间复杂度O(m*n),空间复杂度O(n)。

=============================================

第二次,用偷懒的做法了,二维dp直接写了。

class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if ( grid.empty() ) return ;
int dp[grid.size()][grid[].size()];
fill_n(&dp[][], grid.size()*grid[].size(), );
dp[][] = grid[][];
for ( int i=; i<grid[].size(); ++i ) dp[][i] = dp[][i-]+grid[][i];
for ( int i=; i<grid.size(); ++i ) dp[i][] = dp[i-][]+grid[i][];
for ( int i=; i<grid.size(); ++i )
{
for ( int j=; j<grid[i].size(); ++j )
{
dp[i][j] = min(dp[i][j-],dp[i-][j])+grid[i][j];
}
}
return dp[grid.size()-][grid[].size()-];
}
};

【Minimum Path Sum】cpp的更多相关文章

  1. leetcode 【 Minimum Path Sum 】python 实现

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

  2. 【Binary Tree Maximum Path Sum】cpp

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  3. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

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

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

  7. 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

    [064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...

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

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

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

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

随机推荐

  1. zblog删除网站后台顶部菜单中的“官方网站”链接

    文件\zb_system\function\c_system_admin.php 注释或删除代码 $topmenus[] = MakeTopMenu("misc", $zbp-&g ...

  2. ArcMap中提取影像数据边界

    1.前言 客户手里有一些经过裁剪的不规则多边形影像数据(如图例所示),希望能批量获取该类影像的边界信息,即影像对应的面信息,边界线信息.这里我们提供一种利用镶嵌数据集Footprint图层的方法来获取 ...

  3. Mysql数据库操作语句总结(三)

    最近一段时间重新学习一下mysql命令行的用法, 这里简单记录一下 参考文章: https://www.cnblogs.com/bluealine/p/7832219.html 个人使用的是mysql ...

  4. @Valid的坑

    @Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前

  5. mybatis-动态sql2

    mybatis的动态sql中常用的有    if     where      foreach    set 项目沿用之前的. 1.dao层添加接口: package com.java1234.map ...

  6. 【Orange Pi Lite2】 ——2《在使用之前的配置》(未完)

    [Orange Pi Lite2] --2<在使用之前的配置> 本文只在博客园发布 在开始前你需要准备的材料与软件 filezilla/或者不 声明 : 本教程适合0基础新手,本章将会介绍 ...

  7. Aizu 0033 Ball(dfs,贪心)

    日文题面...题意:是把一连串的有编号的球往左或者往右边放.问能不能两边都升序. 记录左边和右边最上面的球编号大小,没有就-1,dfs往能放的上面放. #include<bits/stdc++. ...

  8. Count Numbers(矩阵快速幂)

    Count Numbers 时间限制: 8 Sec  内存限制: 128 MB提交: 43  解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...

  9. AngularJs学习笔记-数据绑定、管道

    数据绑定.管道 (1)数据绑定(Angular中默认是单向绑定) 1.[]方括号 可以用于子组件传值 由于是单向绑定,所以当子组件中的iStars属性发生改变时,不会影响到父组件中product.ra ...

  10. CUDA的软件体系

    CUDA的软件堆栈由以下三层构成:CUDA Library.CUDA runtime API.CUDA driver API,如图所示,CUDA的核心是CUDA C语言,它包含对C语言的最小扩展集和一 ...