LeetCode——minimum-path-sum
Question
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.
Solution
动态规划。 p[i][j] = grid[i][j] + min(p[i - 1][j], p[i][j - 1]). 因为到达一个节点,最多有两种选择,当然是选择代价较小的。
时间复杂度O(n^2)。
Code
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
if (grid.size() <= 0)
return 0;
vector<vector<int>> tb(grid.size(), vector<int>(grid[0].size(), 0));
int row = grid.size();
int col = grid[0].size();
tb[0][0] = grid[0][0];
// 注意初始化是一个代价累加的过程
for (int i = 1; i < row; i++) {
tb[i][0] = grid[i][0] + tb[i - 1][0];
}
for (int i = 1; i < col; i++) {
tb[0][i] = grid[0][i] + tb[0][i - 1];
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < col; j++) {
tb[i][j] = grid[i][j] + min(tb[i - 1][j], tb[i][j - 1]);
}
}
return tb[row - 1][col - 1];
}
};
LeetCode——minimum-path-sum的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- 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 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Leetcode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- LeetCode:Minimum Path Sum(网格最大路径和)
题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...
- LeetCode Minimum Path Sum (简单DP)
题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...
- [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 ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- mysql创建用户,以及分配权限
创建用户create user test identified by '123456'; 删除用户DROP USER ''@'localhost'; //删除所有host为localhost的用户 给 ...
- 为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
pprof是个神马玩意儿? pprof - manual page for pprof (part of gperftools) 是gperftools工具的一部分 gperftools又是啥? Th ...
- Java 8 : Predicate和Consumer接口
1.consumer jdk 1.8 的 Iterable 接口中的 forEach 默认方法: public interface Iterable<T> { default void f ...
- 011-Shell 文件包含
和其他语言一样,Shell 也可以包含外部脚本.这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空 ...
- java 多线程 day12 读写锁
import java.util.Random;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent. ...
- 解决shell脚本参数传递含有空格的问题
有这样一个py文件,需要传一个字典作为参数: import json import sys def parse_params(data): json_data = json.loads(data[1] ...
- Spring第九弹—使用CGLIIB实现AOP功能与AOP概念解释
JDK自从1.3版本开始,就引入了动态代理,并且经常被用来动态地创建代理,原理之前我已经讲过.JDK的动态代理用起来非常简单,但它有一个限制,就是使用动态代理的对象必须实现一个或多个接口.如果想代理没 ...
- 创建Java不可变型的枚举类型Gender
创建Java不可变型的枚举类型,其实例如下: // 创建不可变型的枚举类 enum Gender { // 此处的枚举值必须调用对应的构造器来创建 MALE("男"), FEMAL ...
- IntBuffer类的基本用法
package com.ietree.basicskill.socket.basic.nio; import java.nio.IntBuffer; /** * Created by Administ ...
- WebStorm keyboard shortcuts
ctrl + D 向下复制 下面是Webstorm的一些常用快捷键: shift + enter: 另起一行 ctrl + alt + L: 格式化代码 control + E: 光标跳到行尾 it ...