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 ...
随机推荐
- 修改pytorch官方实例适用于自己的二分类迁移学习项目
本demo从pytorch官方的迁移学习示例修改而来,增加了以下功能: 根据AUC来迭代最优参数: 五折交叉验证: 输出验证集错误分类图片: 输出分类报告并保存AUC结果图片. import os i ...
- canvas 制作表情包
canvas 制作表情包 代码如下. <!DOCTYPE html> <html> <head> <title>表情制作</title> & ...
- 初学Splunk
splunk简介 https://www.splunk.com/zh-hans_cn/download.html splunk 简体中文版手册 http://docs.splunk.com/Docum ...
- Kubernetes中的Ingress
Ingress是什么 Ingress :简单理解就是个规则定义:比如说某个域名对应某个 service,即当某个域名的请求进来时转发给某个 service;这个规则将与 Ingress Control ...
- 清除input框的缓存
html <div class="container"> <form class="parent" autocomplete="of ...
- 我所用过的nginx的功能
前言 当我们提起集群时,一般所用的插件就是nginx.nginx功能如今越来越完善.第三方模块也多如牛毛,在此,总结一下不牵扯第三方模块所具有的功能. 基本功能 反向代理 负载均衡 HTTP服务器(动 ...
- aioysql(异步操作MySQL)-python
python异步IO初探 探索异步IO执之前,先说说IO的种类 阻塞IO最简单,即读写数据时,需要等待操作完成,才能继续执行.进阶的做法就是用多线程来处理需要IO的部分,缺点是开销会有些大. 非阻塞I ...
- Python学习 :深浅拷贝
深浅拷贝 一.浅拷贝 只拷贝第一层数据(不可变的数据类型),并创建新的内存空间进行储蓄,例如:字符串.整型.布尔 除了字符串以及整型,复杂的数据类型都使用一个共享的内存空间,例如:列表 列表使用的是同 ...
- springMVC3
复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器:根据url查找Handler.(可以 ...
- javaWeb总结
url传值时:如out.println("<td><a href = 'delete.jsp?user=" + user + "'>删除</ ...