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 解题报告的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  3. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  4. 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  5. LeetCode 976 Largest Perimeter Triangle 解题报告

    题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  6. LeetCode: Pascal's Triangle 解题报告

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

  7. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  8. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  9. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

随机推荐

  1. ThinkPHP+jQuery EasyUI Datagrid查询数据的简单处理

    ThinkPHP和jQuery EasyUI这两个都是不错的框架,现在要把它两个整合到一块,做个简单的Ajax调用查询. 在ThinkPHP模板中引入EasyUI的相关文件,然后设置按钮3的调用: & ...

  2. protobuf3 iOS 接入 protobuf

    1.引入官方基础pod 谷歌将protobuf需要使用的基础类封装成了一个pod,因此可以直接安装该pod,不必再手工导入. 如下: pod "Protobuf", :git =& ...

  3. React(0.13) 定义一个使用动画

    <!DOCTYPE html> <html> <head> <title>React JS</title> <script src=& ...

  4. 微信支付中的jsapi返回提示信息

    jsapi中跳转到微信支付中触发的方法是js中的getBrandWCPayRequest方法. 改方法中的返回结果msg提示信息如下: err_msg:get_brand_wcpay_request: ...

  5. Sqlserver大数据量分区表创建

    /* 逆向删除对象 DROP PARTITION SCHEME [PS_BasicPolicy2014]; DROP PARTITION FUNCTION [PF_BasicPolicy2014]; ...

  6. Swift 下标脚本

    前言 在访问一个数组实例的元素时,可以使用 Array[index] 的形式.在访问一个字典实例的元素时,可以使用 Dictionary[index] 的形式.这种方括号的形式就是 "下标脚 ...

  7. MS SQL Server查询优化方法 查询速度慢的原因很多,常见如下几种

    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 5.网络速度慢 6.查询出的数据量过大 ...

  8. Oracle VPD策略示例

    1.未创建前使用oe用户登录查询: SQL> select * from orders; ORDER_ID ORDER_DATE ORDER_MO CUSTOMER_ID ORDER_STATU ...

  9. threaded_execution

    Property Description Parameter type Boolean Default value false Modifiable No Range of values true | ...

  10. PowerDsigner 16逆向工程导入mysql

    由于日常数据建模经常使用PowerDesigner,使用逆向工程能更加快速的生成模型提高效率,所以总结使用如下: 1.      安装MYSQL的ODBC驱动 Connector/ODBC 5.1.1 ...