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.

递归:

 1         public static int MinimumPathSum(List<List<int>> grid)
 2         {
 3             int m = grid.Count;
 4             int n = grid[0].Count;
 5
 6             return MinimumPathSumRecursive(grid, m - 1, n - 1);
 7         }
 8
 9         public static int MinimumPathSumRecursive(List<List<int>> grid, int x, int y)
10         {
11             if (x == 0 && y == 0)
12             {
13                 return grid[0][0];
14             }
15             else if (x == 0)
16             {
17                 return MinimumPathSumRecursive(grid, 0, y - 1) + grid[0][y];
18             }
19             else if (y == 0)
20             {
21                 return MinimumPathSumRecursive(grid, x - 1, 0) + grid[x][0]; ;
22             }
23             else
24             {
25                 return Math.Min(MinimumPathSumRecursive(grid, x - 1, y), MinimumPathSumRecursive(grid, x, y - 1)) + grid[x][y];
26             }
27         }

DP:

 1         public static int MinimumPathSumDP(List<List<int>> grid)
 2         {
 3             int m = grid.Count;
 4             int n = grid[0].Count;
 5
 6             int[,] ret = new int[m, n];
 7
 8             for (int i = 0; i < m; i++)
 9             {
10                 for (int j = 0; j < n; j++)
11                 {
12                     if (i == 0 && j == 0)
13                     {
14                         ret[i, j] = grid[0][0];
15                         continue;
16                     }
17                     if (i == 0)
18                     {
19                         ret[i, j] = ret[i, j - 1] + grid[i][j];
20                         continue;
21                     }
22                     if (j == 0)
23                     {
24                         ret[i, j] = ret[i - 1, j] + grid[i][j];
25                         continue;
26                     }
27
28                     if (ret[i - 1, j] <= ret[i, j - 1])
29                     {
30                         ret[i, j] = ret[i - 1, j] + grid[i][j];
31                         continue;
32                     }
33                     else
34                     {
35                         ret[i, j] = ret[i, j - 1] + grid[i][j];
36                         continue;
37                     }
38
39                 }
40             }
41
42             return ret[m - 1, n - 1];
43         }

代码分析:

  经典的递归(Top-down),DP(Bottom-up)题。DP一般都比递归来的效率高,主要是每次function 调用都新创建一个stack,但是有些问题递归用的巧,也是让人为之一震!

  

转载于:https://www.cnblogs.com/etcow/archive/2012/09/29/2707935.html

LeetCode Online Judge 题目C# 练习 - Minimum Path Sum相关推荐

  1. [leetcode] online judge题目汇总

    最近在leetcode online judge刷题,132道题也做了一半多,略有心得.另外这个online judge对题目没有分类,也没有说明难度,新人上手无法由浅入深,也不能集中练习某些类型的题 ...

  2. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

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

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

  5. 63. Unique Paths II and 64. Minimum Path Sum

    文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...

  6. 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. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

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

  8. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

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

  9. LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

    大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...

最新文章

  1. 使用DataReader、DataSet、DataAdapter和DataView
  2. 中画图title函数_MATLAB-基础画图meshgrid
  3. NeHe教程Qt实现——lesson08
  4. EasyDarwin开源社区 短视频拍摄项目Github地址
  5. python爬百度翻译-教大家用python爬取百度翻译,超简单
  6. 什么是 jQuery 事件
  7. 第一次会议(2019/02/22)
  8. python描述符魔术方法_Python类型转换的魔术方法详解
  9. 等宽字体与非等宽字体_我最喜欢的等宽字体
  10. 360linux如何卸载,卸载360安全卫士方法
  11. 易语言与stc89c52通信,51单片机激光雕刻机制作 含易语言上位机源码等资料
  12. Chrome FeHelper 插件下载地址
  13. 对称密钥算法与非对称密钥算法
  14. 怎么快速找到:附近的人
  15. 酒店:二维码如何应用于酒店管理
  16. thinkpad E450/550 预装系统改装WIN7全套教程
  17. 计算机行业未来20年前景,未来20年,哪个专业最有“前景“?符合一定要报
  18. 计算机桌面进入安全模式,win7安全模式无法入桌面怎么办?无法进入桌面解法...
  19. ToggleSwitch控件介绍
  20. Daily English - ... is driving me up a wall.

热门文章

  1. 如果修改了表结构的话,可能也需要将调用到表的存储过程、函数等也修改一下,以下语句可以查询到那些对象调用到被修改的表...
  2. phpstrom php出现404
  3. Microsoft Bot Framework 上手
  4. 使用渲染纹理的制作摄像头
  5. 为什么那么多人工作都不开心?
  6. 【Linux】ubuntu下词典软件Goldendict介绍(可屏幕取词)和StarDict(星际译王)的安装...
  7. f分布表完整图a=0.05_MySQL8.0新特性-invisible indexes
  8. .sh文件是什么语言_shell命令在C语言程序中的调用
  9. Windows PE导出表编程4(重构导出表实现私有函数导出)
  10. 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 五 )