【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 ...
随机推荐
- nmon 工具的使用
原文链接:https://www.ibm.com/developerworks/cn/aix/library/analyze_aix/ 引言 nmon 工具可以为 AIX 和 Linux 性能专家提供 ...
- Cloneable接口的作用
Cloneable接口是一个[标记接口],就是没有任何内容 implements Cloneable表示该对象能被克隆,能使用Object.clone()方法.如果没有implements Clone ...
- SAP成都研究院CEC团队三巨头之一:M君的文章预告
国人总倾向于把特点或者作用类似的人或物放在一起比较并做出排名,于是就有了许多"某某某三巨头"的称谓. 最举世闻名的莫过于二战三巨头:丘吉尔,罗斯福和斯大林. 还有陪伴咱八零后童年时 ...
- 如何解析比特币中的交易原始数据rawData
交易数据结构 有关交易的详细信息可以查看比特币的wiki网站:Transaction TxBinaryMap: 原始图片地址 交易的结构表格(Transaction): 示例数据 以一个正式网络的一笔 ...
- Aizu 0033 Ball(dfs,贪心)
日文题面...题意:是把一连串的有编号的球往左或者往右边放.问能不能两边都升序. 记录左边和右边最上面的球编号大小,没有就-1,dfs往能放的上面放. #include<bits/stdc++. ...
- 【51nod1743】雪之国度(最小生成树+倍增)
点此看题面 大致题意: 给你一张无向连通图,其中每条边的边权为这条边连接的两点的权值之差.每次询问两点之间是否存在两条不重复的路径,若存在则输出这两条路径上最大值的最小值. 大致思路 这题显然就是要让 ...
- 【BZOJ1040】[ZJOI2008] 骑士(基环外向树DP)
点此看题面 大致题意: 给你一片基环外向树森林,如果选定了一个点,就不能选择与其相邻的节点.求选中点的最大权值和. 树形\(DP\) 此题应该是 树形\(DP\) 的一个升级版:基环外向树\(DP\) ...
- CPP-基础:C_C++变量命名规则
C_C++变量命名规则 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++必须遵守的变量命名规则: 1. 变量名只能是字母(A-Z,a-z)和数字(0-9)或者下划线(_)组成. 2. 第一 ...
- 【wiki】红帽linux
Red Hat Enterprise Linux From Wikipedia, the free encyclopedia wiki 上面红帽的版本信息. https://en.wikipedia. ...
- MyBatis的discriminator鉴别器根据字段值实现Java中的多态
<select id="getModelById" resultMap="modelTypeMap"> SELECT id as id, model ...