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 ...
随机推荐
- JavaScript学习笔记(4)——JavaScript语法之变量
一.变量可以使用短名称(比如 x 和 y),也可以使用描述性更好的名称(比如 age, sum, totalvolume). 变量必须以字母开头 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做 ...
- C++ map.insert 传参类型不同,构造/析构次数不同
1. 传参方式 使用 insert 为 map 插值时,insert 的传参包含以下几种可能: make_pair 生成对象 pair(key_type, value_type) 生成对象 pair( ...
- mysql 1045的的解决方案
找到配置文件my.ini ,然后将其打开,可以选择用记事本打开 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tables,保存退出. 然后重启mysql服务 ...
- STM32F40xxx 与 STM32F41xxx Flash结构详解
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 硬件平台:STM32F4 DISCOVERY开发板 型号:MB997A或MB997C主芯片型号:ST ...
- debian 学习记录-3 -关于linux -1
来源:<Debian标准教程>王旭 著 芬兰人Linus Trovalds 1991年1月2日···· 2006年初发布内核2.6.15 使用Andrew Tanenbaum < ...
- Linux C 程序 字符串运算符-表达式(TWO)
1.字符串常量 双引号"" :eg:"china" ,字符串在存储的时候会以一个\0为结束标志.2.符号常量 ,给常量取一个名字. #include< ...
- this在JavaScript中的工作范围
this在JavaScript中的工作范围 在一个函数中,this的行为,取决于JavaScript函数的调用方式和定义方式,而不仅仅是看它如何被定义的. var fullname = 'Fu';va ...
- HTTP协议(4)
HTTP 概括总结 方便以后使用.遗忘时有侧重点的去学习,方便查阅: 开始看到webservice 和restful 有些不理解 现在可以简单理解为 : webservice = http协议+XM ...
- 多行滚动jQuery循环新闻列表代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【Druid】 阿里巴巴推出的国产数据库连接池com.alibaba.druid.pool.DruidDataSource
阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好 简单使用介绍 Druid与其他数据库连接池使用方法基本一样(与DBCP非常相似),将数据库的连接信息 ...