【Minimum Path Sum】cpp
题目:
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的更多相关文章
- 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 ...
- 【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 ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】
[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
随机推荐
- Team Foundation 版本控制
与 Visual Studio 的一流集成. 使用富文件和文件夹差异工具突出显示代码更改. 借助强大的可视化跨分支跟踪代码更改. 集成的代码评审工具有助于在签入代码之前获得反馈. 使用托管版本或本地版 ...
- System Center Configuration Manager 2016 域准备篇(Part2)
对于" 服务器角色",请选择" Active Directory域服务",当系统提示您添加Active Directory域服务所需的功能时,请选择" ...
- Object.keys 函数 (JavaScript)
Object.keys 函数 (JavaScript) 返回对象的可枚举属性和方法的名称. 在实际开发中,我们有时需要知道对象的所有属性,原生js给我们提供了一个很好的方法:Object.keys() ...
- World Wind Java开发之九——阶段小结(转)
http://blog.csdn.net/giser_whu/article/details/42785875 将近一个月没有更新了,一是因为项目的事情,二是期末考试复习,三是玩啦.上一篇博客搭建起了 ...
- go语言,安装包fetch error 问题解决方案
最近需要安装grequests,出现了下面的error [fdf@zxmrlc ~]$ go get github.com/levigross/grequests package golang.org ...
- 在Spark集群中,集群的节点个数、RDD分区个数、cpu内核个数三者与并行度的关系
梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...
- NBU基本常用命令
Veritas常用命令: 1. 查看当有运行的任务 bpdbjobs –report | grep Active 2. 停止任务 bpdbjobs –cancel PID (包括主任务和子任务) 3. ...
- Zuul 网关搭建
本机IP为 192.168.1.102 1. 新建 Maven 项目 zuul 2. pom.xml <project xmlns="http://maven.apac ...
- Shell重启Tomcat脚本
#!/bin/bash echo -e "\n\n\n" #force kill flag,if equal [f] to force kill all flag="He ...
- phalcon中find 最常用
1 官网:http://docs.iphalcon.cn/reference/models.html#finding-records 2 具体操作实例 数据: mysql> select * f ...