leetcode 120 Triangle ----- java
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 ...
随机推荐
- 【C语言学习】-03 循环结构
本文目录 循环结构的特点 while循环 do...while循环 for循环 回到顶部 一.循环结构的特点 程序的三种结构: 顺序结构:顺序执行语句 分支结构:通过进行一个判断在两个可选的语句序列之 ...
- URAL 1519 基础插头DP
题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...
- SPOJ SUBLEX 求第k小子串
题目大意: 对于一个给定字符串,找到其所有不同的子串中排第k小的子串 先构建后缀自动机,然后我们可以将整个后缀自动机看做是一个DAG图,那么我们先进行拓扑排序得到 *b[N] 对于每个节点记录一个sc ...
- SharePoint开发 - 自定义页面(错误页、登出页)
博客地址 http://blog.csdn.net/foxdave 本文叙述如何自定义SharePoint的固有页面,比较简单,用一句话说就是"做个页面,写一句代码." 创建Sha ...
- JAVA SERVLET专题(上)
SERVLET简介 ·Java Servlet 是和平台无关的服务器端组件,它运行在Servlet容器中.Servlet容器负责Servlet和客户的通信以及调用Servlet的方法,Servlet和 ...
- solr学习之入门篇
一,简介 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http ...
- 2016/7/30 div位置设置,表格大小统一
在做作业过程中遇到的一些问题和我的总结 1.作业中给出了一张背景图,并让我在背景图中一片区域写内容 我的思路是将一个div放在这片区域,那么问题就是如何定义div的位置和大小 查阅资料后发现并不复杂, ...
- FragmentActivity+FragmentTabHost+Fragement instead of TabActibvity+TabHost+Activity
http://www.tuicool.com/articles/NzeMJz http://www.tuicool.com/articles/FJ7VBb FragmentTabHost切换Fragm ...
- 极客DIY:RFID飞贼打造一款远距离渗透利器
本文使用最新的渗透工具RFID飞贼(Tastic RFID Thief)和RFID感应破解技术来获取一些拥有安防的建筑物的访问权限. Tastic RFID Thief是一个无声远距离RFID读卡器, ...
- electronic data interchange 电子数据交换
electronic data interchange 电子数据交换