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. CentOS7 minimal 没有netstat命令

    在CentOS 7 minimal中使用netstat 时,发现显示如下,明显没有了netstat 命令 [root@localhost ~]# netstat -a -bash: netstat: ...

  2. ElasticSearch : 基础

    #新建索引以及类型: PUT http://10.18.43.3:9200/test { "settings": { "number_of_shards": 3 ...

  3. 1. tty终端接收数据原理

    1. 串口接收数据原理(以“ls”命令为例) (1) 每个终端设备都有一个接收缓冲区,保存原始数据.shell程序一直在休眠,等待合适的字符: (2) 串口接收到'l'字符,把它保存下来: (3) 串 ...

  4. 网站安全检测 漏洞检测 对thinkphp通杀漏洞利用与修复建议

    thinkphp在国内来说,很多站长以及平台都在使用这套开源的系统来建站,为什么会这么深受大家的喜欢,第一开源,便捷,高效,生成静态化html,第二框架性的易于开发php架构,很多第三方的插件以及第三 ...

  5. latex01-LaTeX环境的安装与配置

    以Tex Live (跨平台的发行版软件)为例. 1.官网下载iso镜像文件 2.用advanced.bat 安装(管理员权限) 选择要安装的包(主要是去掉多余的语言包) 测试Tex Live 是否正 ...

  6. HDOJ 1176 免费馅饼(完全背包)

    参考:https://blog.csdn.net/hhu1506010220/article/details/52369785 https://blog.csdn.net/enjoying_scien ...

  7. SGU 495

    #include<bits/stdc++.h> using namespace std; #define ll long long ; ; int n,m; double dp[N]; / ...

  8. [JSOI2007] 建筑抢修 (贪心 + 优先队列)

    小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会 ...

  9. 39-Role以及Claims授权

    asp.net core多鼓励使用claims授权 1-使用role授权 在类或方法上贴上Roles,这样就知道有user的角色才可以访问 [Authorize(Roles="user&qu ...

  10. 深度学习:激活函数的比较和优缺点,sigmoid,tanh,relu

    https://blog.csdn.net/u011684265/article/details/78039280