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.

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

思路:经典DP动态规划入门问题,

定义状态转移方程f[i][j] = min(f[i-1][j],f[i][j-1])+grid[i][j]

初始状态函数,二维数组f[][]的第一行和第一列

========

code 如下:

class Solution{
public:
int minPathSum(vector<vector<int> > &grid){
if(grid.size()==) return ;
const int m = grid.size();
const int n = grid[].size(); int f[m][n];
f[][] = grid[][];
for(int i = ;i<m;i++){
//初始化状态方程第一lie
f[i][] = f[i-][]+grid[i][];
}
for(int i = ;i<n;i++){
//初始化状态方程第一hang
f[][i] = f[][i-]+grid[][i];
} //运用状态方程求解
for(int i = ;i<m;i++){
for(int j = ;j<n;j++){
if(f[i-][j]<f[i][j-]){
cout<<i-<<"-"<<j<<endl;
}else{
cout<<i<<"-"<<j-<<endl;
}
f[i][j] = min(f[i-][j],f[i][j-])+grid[i][j];
}
} return f[m-][n-];
}
};

64. Minimum Path Sum的更多相关文章

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

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

  2. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

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

  4. [LeetCode] 64. Minimum Path Sum 最小路径和

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

  5. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  6. C#解leetcode 64. Minimum Path Sum

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

  7. LeetCode OJ 64. Minimum Path Sum

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

  8. LeetCode 64. Minimum Path Sum(最小和的路径)

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

  9. 64. Minimum Path Sum(中等, 又做出一个DP题, 你们非问我开不开心,当然开心喽!^^)

    Given an m x n grid filled with nonnegative numbers, find a path from top left to bottom right which ...

随机推荐

  1. 2015GitWebRTC编译实录5

    2015.07.20 libaudio_encoder_interface/libaudio_decoder_interface 编译通过将encoder,decoder两个lib合并了,后面需要看看 ...

  2. Apahce的虚拟用户认证及server-status页

    一.Apache虚拟用户认证配置 编辑配置文件加入如下内容: <Directory "/www/htdoc/fin"> Options None AllowOverri ...

  3. Codeforces Round #143 (Div. 2)

    A. Team 模拟. B. Magic, Wizardry and Wonders 可以发现\[d=a_1-a_2+a_3-a_4+\cdots\] 那么有\(odd=\lfloor \frac{n ...

  4. eclipse使用jetty插件出现内存溢出解决方案

    系统运行在MAVEN中的jetty插件下,当在ECLIPSE运 clean jetty:run时系统提示 OutOfMemoryError: PermGen space. 解决办法:设置run as ...

  5. 【python2.7】raw_input()和input()区别及用法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一.函数介绍1. input([prompt]) 等同于eval(raw_input([prompt])),这个函数不会捕捉用户输入上的错误,如果 ...

  6. 亲测 logminer挖掘

    LogMiner两种使用类型,一种是使用源数据库的数据字典分析DML操作,别一种是摘取LogMiner数据字典到字典文件分析DDL操作.检查下suppplemental logging:SQL> ...

  7. Sklearn库例子3:分类——岭回归分类(Ridge Regression )例子

    为了解决数据的特征比样本点还多的情况,统计学家引入了岭回归. 岭回归通过施加一个惩罚系数的大小解决了一些普通最小二乘的问题.回归系数最大限度地减少了一个惩罚的误差平方和. 这里是一个复杂的参数,用来控 ...

  8. Python使用MySQL数据库的方法以及一个实例

    使用环境:Windows+python3.4+MySQL5.5+Navicat 一.创建连接 1.准备工作,想要使用Python操作MySQL,首先需要安装MySQL-Python的包,在Python ...

  9. ExtJs 入门教程

    http://www.cnblogs.com/iamlilinfeng/archive/2012/12/31/2840663.html

  10. IE10、IE11 无法写入Cookie

    IE10.IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题 你是否遇到过当使用一个涉及到Cookie操作的网站或者管理系统时,IE 6.7.8.9下都跑的好好的, ...