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. C语言学习笔记 (002) - C++中引用和指针的区别(转载)

    下面用通俗易懂的话来概述一下: 指针-对于一个类型T,T*就是指向T的指针类型,也即一个T*类型的变量能够保存一个T对象的地址,而类型T是可以加一些限定词的,如const.volatile等等.见下图 ...

  2. FluentScheduler

    The job configuration is handled in a Registry class. A job is either an Action or a class that inhe ...

  3. 关于tensorboard启动问题

    我在学习过程中遇到了tensorboard无法启动的问题. 按照网上的教程,我无法正常启动tensorboard,全过程没有报错,但是打开tensorboard显示 No dashboards are ...

  4. CListCtrl自适应宽度

    原文链接: http://blog.csdn.net/benny5609/article/details/1967084 void CListCtrlExDlg::AdjustColumnWidth( ...

  5. 如何将shell的打印日志输入到日志文件

    如果shell打印的日志很多,屏幕无法完全显示,需要查看shell执行的情况,这是就需要输入到日值了: 如:echo "2012-6-14" | tee -a my.log -a表 ...

  6. ReactNative 环境的搭建和启动(安卓版)

    一.JAVA环境 下载 JDK 8.0 添加 %JAVA_HOME% 变量 添加 PATH:%JAVA_HOME%\bin 二.Android环境 下载 Android SDK 修复 SDK Mana ...

  7. Android Activity标签属性

    Android Activity标签属性 Activity 是 Android 系统四大应用组件之一,用户可与 Activity 提供的屏幕进行交互,以执行拨打电话.拍摄照片.发送电子邮件等操作开发者 ...

  8. Flume 相关

    在CentOS 7上安装配置Flume https://mos.meituan.com/library/41/how-to-install-flume-on-centos7/ Flume NG 学习笔 ...

  9. 分布式服务管理框架-Zookeeper节点ACL

    文章转自:http://blog.csdn.net/xyang81/article/details/53147894 概述 ACL全称为Access Control List(访问控制列表),用于控制 ...

  10. MySql(十一):MySQL性能调优——常用存储引擎优化

    一.前言 MySQL 提供的非常丰富的存储引擎种类供大家选择,有多种选择固然是好事,但是需要我们理解掌握的知识也会增加很多.本章将介绍最为常用的两种存储引擎进行针对性的优化建议. 二.MyISAM存储 ...