LeetCode -- Tiangle
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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 因为之前完全没有接触过Spring,所以准备先把spring实战看完再落实项目
因为之前完全没有接触过Spring,所以准备先把spring实战看完再落实项目
- yum仓库客户端搭建和NTP时间同步客户端配置
一.yum仓库客户端搭建 yum源仓库搭建分为服务器端和客户端. 服务端主要提供软件(rpm包)和yumlist.也就是提供yum源的位置.一般是通过http或者ftp提供位置. 客户端的配置:yum ...
- 【PHP】array_column函数
array_column() 返回输入数组中某个单一列的值. 例子,从记录集中取出 last_name 列: <?php // 表示由数据库返回的可能记录集的数组 $a = array( arr ...
- 廖老师的Python教程——Python简介
一直想了解下Python,今儿在外面办事排队的时候,打开了廖老师的官网,找到了Python教程.虽然只是一篇关于Python的简介,但是通过将Python的特性与C进行对比,很生动地归纳了Python ...
- 第三章 最简单的C程序设计——顺序程序设计
一.数据的表现形式及其运算 1.常量和变量 在计算机高级语言中,数据有两种表现形式:常量和变量. 1.1.常量 在程序运行过程中,其值不能被改变的量称为常量.如:5,6,32,0.111. 数值常量就 ...
- TopCoder SRM 489 Div1 Lev3:AppleTree
挺优秀的一道题,想出做法时有些惊艳. 题意: 数轴上有\(D\)个连续整数刻度,有\(N\)棵树要种在这些刻度上,其中第\(i\)棵与两旁(如果有的话)相邻的树至少要相距\(R_i\),问方法数. \ ...
- ChipScope软件使用
内容组织 1.建立工程 2.插入及配置核 2.1运行Synthesize 2.2新建cdc文件 2.3 ILA核的配置 3. Implement and generate programmi ...
- Spark 实践
1.1 避免使用 GroupByKey 让我们看一下使用两种不同的方式去计算单词的个数,第一种方式使用 reduceByKey, 另外一种方式使用 groupByKey: val words = ...
- WPF中使用定时器的注意事项
原文:WPF中使用定时器的注意事项 注意事项 要使用System.Windows.Threading.DispatcherTimer,而不能使用System.Timers.Timer. 原因是WPF是 ...
- Tapestry 权威讲解-备份
http://blog.csdn.net/mindhawk/article/details/5021371#introduction