[LeetCode][Java]Triangle@LeetCode
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.
Runtime: 444 ms
动态规划思想: 从上到下一行一行的扫描并加入总体,dp[i]记录每扫描完一行,并途过本行i元素的最短路径,故dp[]记录到目前为止所有路径的长度。O(n) space
public class Solution {
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
int[] dp=new int[triangle.size()];
if(triangle.size()==0) return 0;
if(triangle.size()==1) return triangle.get(0).get(0);
dp[0]=triangle.get(0).get(0);
for(int i=1;i<triangle.size();i++){
for(int j=i;j>=0;j--){
if(j==0) dp[0]+=triangle.get(i).get(0);
else if(j<i) dp[j]=triangle.get(i).get(j)+ Math.min(dp[j],dp[j-1]);
else dp[j]=dp[j-1]+triangle.get(i).get(j);
}
}
int ret=Integer.MAX_VALUE;
for(int i=0;i<dp.length;i++){
if(dp[i]<ret) ret=dp[i];
}
return ret;
}
}
[LeetCode][Java]Triangle@LeetCode的更多相关文章
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- IIS 出现如下错误:PageHandlerFactory-Integrated”
原因: vs2010默认的是4.0框架,4.0的框架是独立的CLR,和2.0的不同,如果想运行4.0的网站,需要用aspnet_regiis注册4.0框架,然后用4.0的Class池,就可以运行4.0 ...
- 策略模式(strategy pattern)
策略模式在java集合中的TreeSet和TreeMap中得到了很好的应用,我们可以实现Comparator接口实现Compareto()方法来定义自己的排序规则,然后通过TreeSet,TreeMa ...
- 配合crond服务实现自定义周期备份MySQL数据库(使用innobackupex进行备份)
备份 新建一个脚本/root/backup.py,内容如下: #!/usr/bin/env python# -*- coding: utf-8 -*- ''' 脚本作者:昨夜星辰 脚本作用:配合cro ...
- hql between and 查询
public IList<PrdtStdEntity> QueryPrdtStd(PrdtStdEntity prdtStdEntity) { try { var hql = " ...
- 如何将Console application的Program函数变成支持async的?
如何将Console application的Program函数变成支持async的? class Program { static void Main(string[] args) { Task ...
- 笔记9-徐 DBCC SHRINKFILE不起作用的原因
1 , , , , , , , , ,40) ,1 page_id pg_alloc ext_size obj_id index_id partition_number partition_id ia ...
- python之路-Day5
1.列表生成式,迭代器&生成器 列表生成式 我现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],我要求你把列表里的每个值加1. 普通版 a = [0,1,2,3 ...
- tomcat配置性能调优1----server.xml文件详解
<?xml version='1.0' encoding='utf-8'?><!-- Licensed to the Apache Software Foundation (ASF ...
- ETL利器Kettle
ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析 ...
- <![CDATA[ ]]> 的作用
在xml文件中 一些特殊字符需要去除其本意,就要用到 <![CDATA[ ]]>,,比如 ibitis的sqlmap.xml 中 要比较大小不能直接用 < 或者 > , ...