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; }
}
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的更多相关文章
-
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 ...
-
LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
-
LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
-
[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 ...
-
[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 ...
-
[leetcode] 120. Triangle (Medium)
原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...
-
LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
-
LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
-
[leetcode]120.Triangle三角矩阵从顶到底的最小路径和
Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...
随机推荐
-
autofac 组件的实例范围
实例范围决定如何在请求之间共享服务. 原文地址:http://docs.autofac.org/en/latest/lifetime/instance-scope.html 每个依赖一个实例 使用这个 ...
-
MySQL: @variable vs. variable. Whats the difference?
MySQL: @variable vs. variable. Whats the difference? up vote351down votefavorite 121 In another qu ...
-
【Alpha版本】冲刺阶段——Day 7
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...
-
BaKoMa Tex Word 的使用
数学论文编排软件,付费,但是可以这么处理,安装好后不要马上打开,进入影子系统的时候再运行它,这样每次都是全新的, 优势是 WYSIWYG,所见即所得, 中文输入, \documentclass{art ...
-
Redis配置文件(redis.conf)说明
Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法3> Redis CONFIG 命令 ...
-
[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的连接恢复和命令拦截
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第四篇:MVC程序中实体框架的连接恢复和 ...
-
HTTP权威指南阅读笔记一:HTTP概述
HTTP协议版本: 1.HTTP/0.9:HTTP的1991原型版本称为HTTP/0.9.这个协议有很多严重的缺陷,只应该用与与老客户端的交互.HTTP/0.9只支持GET方法,不支持多媒体内容的MI ...
-
WLAN频段介绍-04
ISM频段 ISM频段,此频段主要是开放给工业.科学.医学三个主要机构使用,该频段是依据美国联邦通讯委员会(FCC)所定义出来,并没有所谓使用授权的限制. 工业频段:美国频段为902-928MHz,欧 ...
-
php获取GET方式传入的全部变量名称与值:foreach用法
$count = count($_GET); $i = 0; foreach ($_GET as $key => $value) { if ($i == $count - 1) { $str . ...
-
Android Adapter的getViewTypeCount和getItemViewType
Adapter的getViewTypeCount和getItemViewType 不同的项目布局(item layout) 我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线 你需要做这些: ...