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 ...
随机推荐
- Java垃圾回收机制_(转载)
Java垃圾回收机制 说到垃圾回收(Garbage Collection,GC),很多人就会自然而然地把它和Java联系起来.在Java中,程序员不需要去关心内存动态分配和垃圾回收的问题,这一切都交给 ...
- WebService开发常用功能详解
一.WebService中常用的属性(Attributes)1. Web Service(Web服务)提供以下三个属性. Namespace:此属性的值包含 XML Web Service的默认 ...
- 解决VS报表.rdl 显示乱码“小方块”问题
报表在编辑状态显示文本显示小方块 如图 原因:字体格式是英文状态下. 解决:选中文本框,选择文本框属性,选择字体,字体改成宋体或微软雅黑.就可以了.
- C#中常用修饰符
1.存取修饰符 public:(公有的)存取不受限制 protected:(受保护的)只有包含该成员的类以及派生类可以存取 private:(私有的)只有包含该成员的类可以使用 2.类修饰符 abs ...
- 【html】【19】高级篇--大事件时间轴
下载: http://sc.chinaz.com/jiaoben/131112181390.htm 其它: http://sc.chinaz.com/tag_jiaoben/shijianzhou.h ...
- nodejs学习[持续更新]
1.退出node process.exit(0) 2.把API从上往下全部看一遍,先混个眼熟. 3. end
- [位运算] [搜索] [递推优化] [计算几何] TEST 2016.7.15
NOIP2014 提高组模拟试题 第一试试题 题目概况: 中文题目名称 合理种植 排队 科技节 源程序文件名 plant.pas/.c/.cpp lineup.pas/.c/.cpp scifest. ...
- dataTable 禁止分页
$("#id").DataTable({ "paging": false, // 禁止分页 });
- ZeroMQ/jzmq安装使用
环境: No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 12.04.2 LTS Release: 12 ...
- Python SqlAlchemy使用方法
1.初始化连接 from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create ...