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

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

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.

求出最小的路径。

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

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {int len = triangle.size(); int result = triangle.get(0).get(0); result = getResult(result,0,0,triangle); return result; } public static int getResult(int result,int pos,int num,List<List<Integer>> triangle){ if( num == triangle.size()-1 )
return result;
int num1 = triangle.get(num+1).get(pos);
int ans = result;
ans += num1;
ans = getResult(ans,pos,num+1,triangle); num1 = triangle.get(num+1).get(pos+1);
result += num1;
result = getResult(result,pos+1,num+1,triangle);
return ans>result?result:ans; }
}
 
所以还是需要用DP。
 
 比较简单的DP应用。
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) { int height = triangle.size(); int[] dp = new int[height];
dp[0] = dp[0]+triangle.get(0).get(0);
for( int i = 1;i<height;i++){
int a = dp[0],b = dp[1];
dp[0] = dp[0]+triangle.get(i).get(0);
for( int j = 1;j<i;j++){
dp[j] = Math.min(a,b)+triangle.get(i).get(j);
a = b;
b = dp[j+1];
}
dp[i] = a+triangle.get(i).get(i);
}
int result = dp[0];
for( int i = 1;i<height;i++)
result = Math.min(result,dp[i]); return result; }
}
 
 

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. LibLinear(SVM包)使用说明之(一)README

    转自:http://blog.csdn.net/zouxy09/article/details/10947323/ LibLinear(SVM包)使用说明之(一)README zouxy09@qq.c ...

  2. HDU 4622 求解区间字符串中的不同子串的个数

    题目大意: 给定一个长度<2000的串,再给最多可达10000的询问区间,求解区间字符串中的不同子串的个数 这里先考虑求解一整个字符串的所有不同子串的方法 对于后缀自动机来说,我们动态往里添加一 ...

  3. vs 折叠跟展开所有方法。

    Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl + M + L: 展开所有方法

  4. Xceed WPF 主题皮肤控件Xceed Professional Themes for WPF详细介绍

    Xceed Professional Themes for WPF是一款为你的整个应用程序提供完美WPF主题风格的控件,包含Office 2007和Windows 7,可以应用到任何微软官方的WPF控 ...

  5. AJAX笔记

    浏览器脚本——AJAX AJAX =  异步的 JavaScript 和 XML(Asynchronous JavaScript and XML). 是一种新的技术,它可以创建更好.更快且交互性更强的 ...

  6. SharePoint 2013 运行在IIS 应用32位错误

    问题描述: 今天有一个项目因为用了OWC11,没有64位的dll,因此IIS设置了“启用32位应用程序”为true. 如图: 详细操作见:http://www.cnblogs.com/cainiaog ...

  7. C#中的五个访问修饰符

    一.public, private, protected, internal, protected internal 1.public : 公开的, 公共的 2.private : 私有的 (只能在当 ...

  8. 2016 - 1 - 20 runloop学习

    一:Runloop基本知识 1.本质就是运行循环 2.基本作用: 2.1保证程序持续运行 2.2处理APP中的各种事件:触摸,定时器,selector... 2.3节省CPU资源,系统程序性能:它会让 ...

  9. 从Wireshark监听的数据中提取需要的数据

    最近,需要将wireshark监听的数据进行提取,分两步:首先,应该得出wireshark的数据包吧,在图形化界面中可以非常直观的将监听数据进行存储,但是这样需要手动操作非常麻烦,而且容易出错(随着处 ...

  10. GSM cell phone calls use outdated encryption that can now be cracked with rainbow tables on a PC

    Decrypting GSM phone calls Motivation. GSM telephony is the world’s most popular communication techn ...