[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 ...
随机推荐
- Nginx 遇到的问题
1.样式没有出来 原因:在Nginx配置中没设置好地址 解决办法:重新设置,如下 #静态资源缓存设置 location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$ { ex ...
- windows服务器的DDOS防御,
抵御 SYN 攻击 SYN 攻击利用了 TCP/IP 连接建立机制中的安全漏洞.要实施 SYN 洪水攻击,攻击者会使用程序发送大量 TCP SYN 请求来填满服务器上的挂起连接队列.这会禁止其他用户建 ...
- Python自动化 【第十七篇】:jQuery介绍
jQuery jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多),对javascript进行了封装,是的更加便捷的开发,并且在 ...
- Redis性能点
AliRedis是来自阿里巴巴的基于Redis改造的缓存产品,目前还未开源.网上只能搜到这么一篇资料<AliRedis单机180w QPS, 8台服务器构建1000w QPS Cache集群&g ...
- DAO实例代码优化
一般在接口的实现类中不用来实现登陆等功能,在测试类中实现并且测试. package com.beiwo.epet.service.impl; import com.beiwo.epet.dao.Mas ...
- SQL语句优化实践
减少查询的影响结果集,避免出现全表扫描. 影响结果集是SQL优化的核心.影响结果集不是查询返回的记录数,而是查询所扫描的结果数.通过Explain或Desc分析SQL,rows列的值即为影响结果集(还 ...
- webdriver对象定位方法
webdriver提供了一系列的对象定位方法,常用的有以下几种 · id · name · class name · link text · partial link text · tag name ...
- Net分布式系统之五:C#使用Redis集群缓存
本文介绍系统缓存组件,采用NOSQL之Redis作为系统缓存层. 一.背景 系统考虑到高并发的使用场景.对于并发提交场景,通过上一章节介绍的RabbitMQ组件解决.对于系统高并发查询,为了提供性能减 ...
- FIleText转换为JSONObject对象
package com.beijxing.TestMain; import java.io.File; import java.io.IOException; import org.apache.co ...
- DIOCP之编写第一个应用程序(三)
Client 设计功能如下: 1.建立与服务器连接 2.请求连接时,加密密码,采用Base64编码 3.定时发送心跳告诉服务器在线(长连接,用于接收推送信息) 4.进行相关的数据处理与交互 第一步:创 ...