【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 ...
随机推荐
- iOS - 协议实现的例子
在实际开发中,协议的应用非常广泛,以下是实际应用的例子. 1.协议的定义: myProtocolDelegate.h // // myProtocolDelegate.h // zlwPlayerAp ...
- display:table的几个用法(元素平分宽度,垂直居中)
DIV+CSS的布局已经让表格布局几乎很少用到,除非表格语义性很强的情况. display:table解决了一部分需要使用表格特性但又不需要表格语义的情况, 尤其是DIV+CSS很不方便解决的问题,比 ...
- 美国移民局的I797表原件和I129表是什么呢
I-129表,Petition for a Non-immigrant Worker,即非移民工作许可申请表I797 表 ,Original L1-1A approval notice L1签证批准通 ...
- 打包ios软件并发布到应用商店
真心感慨程序员是一个神奇的动物. 昨天接到任务,将项目打包并发布到apple商店.于是乎... 利用Hbuilder打包 需要的3个文件: AppId,描述文件profile,以及私钥证书 必须条件: ...
- POJ 3187 Backward Digit Sums (递推,bruteforce)
第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...
- Problem G: 角谷步数
Problem G: 角谷步数 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 78 Solved: 28[Submit][Status][Web Bo ...
- C# sizeof运算符
一.C# sizeof运算符 sizeof运算符用于获取值类型的字节数. 二.示例 using System;using System.Collections.Generic;using System ...
- Java 窗体的基本操作语句 JFrame
package com.swift; import java.awt.Color; import java.awt.GridLayout; import java.util.Random; impor ...
- Android驱动开发读书笔记七
第七章 (一)创建设备文件 1.使用cdev_init函数初始化cdec 描述设备文件需要一个cdev结构体,代码如下: struct cdev{ struct kobject kobj; struc ...
- 1043: [HAOI2008]下落的圆盘
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1725 Solved: 743[Submit][Status][Discuss] Descripti ...