leetcode面试准备:Triangle
leetcode面试准备:Triangle
1 题目
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).
接口:public int minimumTotal(List<List<Integer>> triangle)
2 思路
题意
注意是三角形,第i
层选了j
位置的数,在第i + 1
层只能够选j
、j + 1
层的数。
这是一道动态规划的题目,求一个三角形二维数组从顶到低端的最小路径和。思路是维护到某一个元素的最小路径和,那么在某一个元素i,j的最小路径和就是它上层对应的相邻两个元素的最小路径和加上自己的值,递推式是sum[i][j]=min(sum[i-1][j-1],sum[i-1][j])+triangle[i][j]
。最后扫描一遍最后一层的路径和,取出最小的即可。每个元素需要维护一次,总共有1+2+...+n=n*(n+1)/2个元素,所以时间复杂度是O(n^2)。而空间上每次只需维护一层即可(因为当前层只用到上一层的元素),所以空间复杂度是O(n)。
上述代码实现时要注意每层第一个和最后一个元素特殊处理一下。
换个角度考虑一下,如果这道题不自顶向下进行动态规划,而是放过来自底向上来规划,递归式只是变成下一层对应的相邻两个元素的最小路径和加上自己的值,原理和上面的方法是相同的,这样考虑到优势在于不需要最后对于所有路径找最小的,而且第一个元素和最后元素也不需要单独处理了,所以代码简洁了很多。代码如下:
3 代码
// 从上往下想
public int minimumTotal(List<List<Integer>> triangle) {
int size = triangle.size();
if (size == 0)
return 0;
int[] sum = new int[size];
sum[0] = triangle.get(0).get(0);
for (int i = 1; i < size; i++) {
int level = i;
sum[i] = sum[i - 1] + triangle.get(i).get(i);
for (int j = level - 1; j >= 1; j--) {
sum[j] = Math.min(sum[j - 1], sum[j]) + triangle.get(i).get(j);
}
sum[0] = sum[0] + triangle.get(i).get(0);
}
// 遍历sum,求最小值
int min = Integer.MAX_VALUE;
for (int s : sum) {
min = Math.min(s, min);
}
return min;
}
// 从下往上想
public int minimumTotal1(List<List<Integer>> triangle) {
int size = triangle.size();
if (size == 0)
return 0;
int[] sum = new int[size];
for (int i = 0; i < size; i++) {
sum[i] = triangle.get(size - 1).get(i);
}
for (int i = size - 2; i >= 0; i--) {
int level = i;
for (int j = 0; j <= level; j++) {
sum[j] = Math.min(sum[j], sum[j + 1]) + triangle.get(i).get(j);
}
}
return sum[0];
}
4 总结
一维DP。模型简单,比较适合在电面中出现。
leetcode面试准备:Triangle的更多相关文章
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- leetcode面试准备: Game of Life
leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- leetcode面试准备:Reverse Words in a String
leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
随机推荐
- Hql 中实用查询时候 引号的使用
出错代码://List vlist = this.getHibernateTemplate().find("from AndroidCustomer ct where ct.token = ...
- ubuntu修改grub背景
grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg
- CocoaLumberjack+XcodeColor(输出带有颜色的日志)在安装过程中遇到的问题
在安装的时候遇到了各种坑,(在这里用到的pch文件的使用以及解决无法引入的问题,可以参考上午的文章) 一(XcodeColor的安装).在github上下载XcodeClolor的插件,并且安装,Xc ...
- thymeleaf的初次使用(带参请求以及调用带参js方法)
之前对于前端框架接触较少,第一次接触thymeleaf,虽说看起来并不复杂但我还是花费了好一会儿才弄懂. 话不多少下面就简单说一下我在项目中的应用. 首先是java代码 controller层 将需要 ...
- mina2.0 spring
Apache MINA是一个网络应用程序框架,它可以帮助用户开发的高性能.高扩展性的网络应用程序.它提供了一个抽象的事件驱动的异步API在不同传输如TCP/IP和UDP/IP通过java NIO. A ...
- svn 项目转移
http://www.cnblogs.com/techMichaelLee/p/3193197.html (参考) svnadmin dump /home/svn/project > /home ...
- 鼠标事件(window.onload的自己的错误)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JSON Date Format/JSON 日期格式方法分享
我是很懒的,不想多说,所以直接上代码.亲们懂的. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://w ...
- LPC17XX 数据手册摘要之系统时钟与功率控制
系统时钟与功率控制 一.系统时钟 LPC17XX有三个独立的时钟振荡器,分别是主振荡器(MIAN_OSC).内部RC振荡器(IRC_OSC).实时时钟振荡器(RTC_OSC).LPC17XX时钟框图如 ...
- 使用weinre通过PC浏览器调试手机网页
Weinre是什么? Weinre代表Web Inspector Remote,是一种远程调试工具.举个例子,在电脑上可以即时的更改手机上对应网页的页面元素.样式表, 或是查看Javascript变量 ...