Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

  1. [
  2. [2],
  3. [3,4],
  4. [6,5,7],
  5. [4,1,8,3]
  6. ]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

求出最小的路径。

第一次做使用递归,超时了。

  1. public class Solution {
  2. public int minimumTotal(List<List<Integer>> triangle) {int len = triangle.size();
  3.  
  4. int result = triangle.get(0).get(0);
  5.  
  6. result = getResult(result,0,0,triangle);
  7.  
  8. return result;
  9.  
  10. }
  11.  
  12. public static int getResult(int result,int pos,int num,List<List<Integer>> triangle){
  13.  
  14. if( num == triangle.size()-1 )
  15. return result;
  16. int num1 = triangle.get(num+1).get(pos);
  17. int ans = result;
  18. ans += num1;
  19. ans = getResult(ans,pos,num+1,triangle);
  20.  
  21. num1 = triangle.get(num+1).get(pos+1);
  22. result += num1;
  23. result = getResult(result,pos+1,num+1,triangle);
  24. return ans>result?result:ans;
  25.  
  26. }
  27. }
 
所以还是需要用DP。
 
 比较简单的DP应用。
  1. public class Solution {
  2. public int minimumTotal(List<List<Integer>> triangle) {
  3.  
  4. int height = triangle.size();
  5.  
  6. int[] dp = new int[height];
  7. dp[0] = dp[0]+triangle.get(0).get(0);
  8. for( int i = 1;i<height;i++){
  9. int a = dp[0],b = dp[1];
  10. dp[0] = dp[0]+triangle.get(i).get(0);
  11. for( int j = 1;j<i;j++){
  12. dp[j] = Math.min(a,b)+triangle.get(i).get(j);
  13. a = b;
  14. b = dp[j+1];
  15. }
  16. dp[i] = a+triangle.get(i).get(i);
  17. }
  18. int result = dp[0];
  19. for( int i = 1;i<height;i++)
  20. result = Math.min(result,dp[i]);
  21.  
  22. return result;
  23.  
  24. }
  25. }
 
 

leetcode 120 Triangle ----- java的更多相关文章

  1. Java for LeetCode 120 Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. LeetCode 120. Triangle (三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. LeetCode - 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. [leetcode 120]triangle 空间O(n)算法

    1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  6. [leetcode] 120. Triangle (Medium)

    原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...

  7. LeetCode 120. Triangle三角形最小路径和 (C++)

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  8. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  9. [leetcode]120.Triangle三角矩阵从顶到底的最小路径和

    Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...

随机推荐

  1. win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面

    错误一: HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (ov ...

  2. C++指针详解

    指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占 ...

  3. 记录一些容易忘记的属性 -- UIButton

    //设置按钮文字字体(这个只在自定义button时有效)    btn1.titleLabel.font = [UIFont systemFontOfSize:30]; showsTouchWhenH ...

  4. hibernate缓存和提高效率

    1.使用二级缓存,多把大批量的.短期多次的查询数据存到二级缓存中,避免和数据库的多次交互,增加负担.二级缓存加在那些增删改少的,查询多的类中.二级缓存的是对象,如果查出来的不是对象,不会放到缓存中去. ...

  5. 《day06---面向对象入门》

    /* java开发流程:思路. 案例:对数组操作.获取最大值. 思路: 1,一组数,要获取最大值,比较. 2,怎么比较?挨个比较,要获取数组中的每一个数据都要比较. 3,比较完,记录下来比较大的数据, ...

  6. 团队博客——Sprint计划会议1

    每日Scrum:第一天 会议时间:4.14.晚八点半 会议地点:基础教学楼一楼大厅 小组成员:郭庆樑,林彦汝,张金 认领人—使团队成员分工合作,保持团队的积极性. ID 名称(NAME) 重要性(IM ...

  7. c++父类指针强制转为子类指针后的测试(帮助理解指针访问成员的本质)(反多态)

    看下面例子: #include "stdafx.h" #include <iostream> class A {  //父类 public: void  f()   / ...

  8. C++实现python标准库中的Counter

    看python standard library by exmple里面提到一个Counter容器,它像muliset一样,能够维持一个集合,并在常量时间插入元素.查询某个元素的个数,而且还提供了一个 ...

  9. 大型HashMap

    看到一篇评估大型HashMap的文章,备份几个Collections库. 原文:Large HashMap overview: JDK, FastUtil, Goldman Sachs, HPPC, ...

  10. HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn

    Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Jav ...