Question:

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).

Analysis:

给出一个三角形,找到从顶部到底部的最小路径。每次跨行移动时只能在相邻的元素间移动。示例如上。

显然用DP,初始条件dp[0][0] = 顶部第一个元素。

状态转移方程为:

dp[i][j] = min(dp[i-1][j-1], dp[i-1][j]) + triangle[i][j];

当然要注意每行的最后一个元素和第一个元素,只有一个来源。

最后再找出最后一行中最小的数值即可。

本来是很简单的,但是由于是List取数时没有数组方便,因此调bug花了一些时间。。。

代码如下:

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0)
return 0;
if(triangle.size() == 1)
return triangle.get(0).get(0);
List<ArrayList<Integer>> dp = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> dp0 = new ArrayList<Integer>();
dp0.add(triangle.get(0).get(0));
dp.add(dp0);
int row = triangle.size();
for(int i=1; i<row; i++ ) {
ArrayList<Integer> dpi = dp.get(i-1);
List<Integer> tempi = triangle.get(i);
ArrayList<Integer> dpii = new ArrayList<Integer>();
int col = tempi.size();
for(int j=0; j<col; j++) {
if(j == 0)
dpii.add(dpi.get(j)+tempi.get(0));
else if(j < dpi.size()){
dpii.add(Math.min(dpi.get(j-1), dpi.get(j)) + tempi.get(j));
}
else
dpii.add(dpi.get(j-1) + tempi.get(j));
}
dp.add(dpii);
}
int min = Integer.MAX_VALUE;
dp0.clear();
dp0 = dp.get(dp.size()-1);
for(int i=0; i<dp0.size(); i++)
if(min > dp0.get(i))
min = dp0.get(i);
return min;
}
}

LeetCode -- Tiangle的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. vertical-align垂直居中

    <div id="content"> <div id="weizi"> 锄禾日当午,<br> 汗滴禾下土.<br> ...

  2. QQ群排名优化到霸屏的策略怎么做?

    谈起QQ群排名霸屏,首先要弄清楚概念,有些刚接触QQ群的朋友可能不太了解,所谓的QQ群排名霸屏,就是指当你的客户群体搜索QQ群某个关键词时,出现在QQ群搜索结果前面的群,全部或者大部分都是我们自己的群 ...

  3. Delphi方法

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  4. Python特别low的一个文字游戏

    闲来无事 ,调侃舍友的游戏 import os class Role(): def __init__(self,name,sex,fighting): self.name=name self.sex= ...

  5. Python学习手册之控制结构(二)

    在上一篇文章中,我们介绍了Python的一些控制结构,现在我们继续介绍剩下的 Python 控制结构.查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/9972 ...

  6. ts包、表、子表、section的关系

    我们经常接触到创建 DEMUX,注册 Filter 过滤数据, 通过回调过滤出 section 数据,然后我们对 section 数据做具体的解析或者其他操作. 我们这里说的 section 就是段的 ...

  7. ts packet解析

    (1)TS流是基于Packet的位流格式,每个包是188字节或者204字节(一般是188字节,204字节的格式仅仅是在188字节的Packet后部加上16字节的CRC数据,其他格式是一样的),整个TS ...

  8. JAVA大作业汇总2

    JAVA大作业2 代码 package thegreatwork; //Enum一般用来表示一组相同类型的常量,这里用于表示运动方向的枚举型常量,每个方向对象包括方向向量. public enum D ...

  9. 隐藏WPF ToolBar 左侧的移动虚线和右侧的小箭头

    原文:隐藏WPF ToolBar 左侧的移动虚线和右侧的小箭头   上面的图是两个工具栏的链接处.   去除蓝色部分的方法是 设置工具栏的ToolBarTray.IsLocked附加选项为True   ...

  10. LI 标签中让文章标题左对齐,日期右对齐的方法

    希望实现标题在左对齐,日期在右对齐,当直接给日期的span加上float:right时,IE8和FF都OK,但IE6/7则会换行,下面给出一个简单有效的解决办法. <!DOCTYPE html  ...