LeetCode: Triangle 解题报告
Triangle
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.
SOLUTION 1:
使用DFS 加记忆矩阵的解法.
mem[i][j]表示第i行第j列的解。它的解可以由下一行推出:mem[i][j] = mem[i+1][j] + mem[i+1][j+1]
/*
REC, SOL 1:
*/
public int minimumTotal1(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return 0;
} int rows = triangle.size();
int[][] mem = new int[rows][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
mem[i][j] = Integer.MAX_VALUE;
}
} return dfs(triangle, 0, 0, mem);
} public int dfs(List<List<Integer>> triangle, int row, int col, int[][] mem) {
if (mem[row][col] != Integer.MAX_VALUE) {
return mem[row][col];
} if (row == triangle.size() - 1) {
mem[row][col] = triangle.get(row).get(col);
} else {
int left = dfs(triangle, row + 1, col, mem);
int right = dfs(triangle, row + 1, col + 1, mem);
mem[row][col] = triangle.get(row).get(col) + Math.min(left, right);
} return mem[row][col];
}
SOLUTION 2:
ref: http://blog.csdn.net/imabluefish/article/details/38656211
动态规划的题目
我们可以轻松将上面的修改为DP.
并且,为了减少内存使用量,使用一维DP即可。
f[j] 表示下一行第j列某点到最后底部的最短值。因为我们只需要下一行的这个值,所以我们使用一行的DP memory即可完成任务。
第一步: 先计算出最后一排的最短值,实际上就是这一排本身的值。
第二步:From bottom to up, 每一层的最短值只需要把自身值加上,并且取下层的左右邻接点的最小值。
/*
DP, SOL 2:
*/
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return 0;
} int rows = triangle.size();
int[] D = new int[rows]; for (int i = rows - 1; i >= 0; i--) {
// 注意:边界条件是 j <= i
for (int j = 0; j <= i; j++) {
if (i == rows - 1) {
D[j] = triangle.get(i).get(j);
} else {
D[j] = triangle.get(i).get(j) + Math.min(D[j], D[j + 1]);
}
}
} return D[0];
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinimumTotal.java
LeetCode: Triangle 解题报告的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- LeetCode: Pascal's Triangle 解题报告
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
随机推荐
- 基于 CoreText 实现高性能 UITableView
引起UITableView卡顿比较常见的原因有cell的层级过多.cell中有触发离屏渲染的代码(譬如:cornerRadius.maskToBounds 同时使用).像素是否对齐.是否使用UITab ...
- SaltStack 入门到精通第二篇:Salt-master配置文件详解
SaltStack 入门到精通第二篇:Salt-master配置文件详解 转自(coocla):http://blog.coocla.org/301.html 原本想要重新翻译salt-mas ...
- Python学习笔记(四)——编码和字符串
一.编码 1.编码类别: (1)ASCII码:127个字母被编码到计算机里,也就是大小写英文字母.数字和一些符号 (2)GB2312码:中国制定的用于加入中文汉字的编码 (3)Unicode:防止由于 ...
- 创建 maven maven-archetype-quickstart 项目抱错问题解决方法
问题: eclipse装m2eclipse的时候装完后创建项目的时候报错: Unable to create project from archetype org.apache.maven.arche ...
- Checkstyle-Configuration
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...
- 【C语言】练习3-5
题目来源:<The C programming language>中的习题P51 练习2-1: 编写函数itob(n, s, b),将整数n转换为以b为底的数,并将转换结果以字符的形 ...
- U盘启动装完系统后 一拔下优盘 就不能进入系统
PE下Ghost安装的,装好进入系统正常,可是拔下 u盘就进不去系统,而插上 u盘就好好的 原因:引导的事,找到本地硬盘第一分区并且激活! 就可以了! 可用下“电脑店-修复主引导记录(MBR)工具”h ...
- maven配置src/resources默认目录
在maven工程中,我们会将配置文件放到,src/main/resources 下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 它编译的路径直接位于classes下面,这个 ...
- U811.1接口EAI系列之五--材料出库--VB语言
主要业务有:09其他出库单 11:材料出库单 32:销售出库单 主要业务代码: '材料出库生成XML Public Function xml_storeout(ds_head As MSHFlexGr ...
- Yii2 使用 faker 生成假数据(转)
测试过程中有时候需要生成大量的假数据,faker 是一个生成假数据的类库,可以生成姓名,电话,IP地址,密码,ISBN等等你能想到的或者你想不到的各种类型的假数据. Yii2.0已经集成该类库,不用再 ...