动态规划—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 is11(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.
思路:
题目的意思是找出从顶到底的最小路径和,类似二叉树。
从下向上进行,例如第i+1行的第j和j+1元素进行比较,将两元素中较小的值与第i行的第j个元素相加,以此类推,最后顶元素就是要求的最小路径和。
代码:
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
int row = triangle.size();
for(int i=row-2;i>=0;i--){
for(int j=0;j<=i;j++){
int min = Math.min(triangle.get(i+1).get(j), triangle.get(i+1).get(j+1));
triangle.get(i).set(j, triangle.get(i).get(j)+min);
}
}
return triangle.get(0).get(0);
}
动态规划—triangle的更多相关文章
- 九章算法系列(#4 Dynamic Programming)-课堂笔记
前言 时隔这么久才发了这篇早在三周前就应该发出来的课堂笔记,由于懒癌犯了,加上各种原因,实在是应该反思.好多课堂上老师说的重要的东西可能细节上有一些急记不住了,但是幸好做了一些笔记,还能够让自己回想起 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- 【动态规划】The Triangle
问题 E: [动态规划]The Triangle 时间限制: 1 Sec 内存限制: 128 MB提交: 24 解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- The Triangle (简单动态规划)
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calc ...
- POJ - 1163 The Triangle 【动态规划】
一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle(动态规划)
题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- LeetCode之“动态规划”:Triangle
题目链接 题目要求: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...
随机推荐
- WPF Microsoft.Practices.Unity 注入大法简单示例
最近新入职了公司,做WPF方向的项目,进来后看到这边大量运用了依赖注入来解耦,采用的是Microsoft.Practices.Unity. WPF的话,目前主要有两个技术来实现IOC,unity和ME ...
- Codeforces Round #603 (Div. 2) E. Editor
E. Editor 题目链接: https://codeforces.com/contest/1263/problem/E 题目大意: 输入一个字符串S1含有‘(’ , ‘)’ , ‘R’ , ‘L’ ...
- IDEA 创建spring boot 的Hello World 项目
1.Open IDEA,choose "New-->Project" 2.Choose "Spring Initializr" 3. Choose jav ...
- 【tensorflow使用笔记三】:tensorflow tutorial中的源码阅读
https://blog.csdn.net/victoriaw/article/details/61195620#t0 input_data 没用的另一种解决方法:tensorflow1.8版本及以上 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_9_字节输入流读取字节数据
硬盘读取到内存 read值会读一个字节 a.txt里面a变成整数就是97 读取到末尾,返回-1 再读一次还是-1 读取的代码是重复的.可以使用循环去读取.while循环. 转行成char类型的 ...
- PCB布线设计-模拟和数字布线的异同(转)
工程领域中的数字设计人员和数字电路板设计专家在不断增加,这反映了行业的发展趋势.尽管对数字设计的重视带来了电子产品的重大发展,但仍然存在,而且还会一直存在一部分与模拟或现实环境接口的电路设计.模拟和数 ...
- spring源码下载及转入ECLIPSE
转自:https://www.cnblogs.com/scevecn/p/6043284.html 本例spring源码版本是4.3.0的, 所以jdk需要准备1.8的(不同版本源码要求的jdk不一样 ...
- [mysql相关集锦] 001 - mysql zip安装/The service already exists/MySQL 服务无法启动
本随笔参考了其他博客内容,且在验证有效之下才或誊抄或摘录或加上自己经验组合而成. 参考博客: 1,https://www.jianshu.com/p/413d29b67422 2,https://bl ...
- JDK8 parallelStream性能测试
https://blog.csdn.net/u011870280/article/details/80700993 public static void main(String[] args) {lo ...
- Spark-Core RDD的创建
一.RDD创建的3种方式: 1.从集合中创建RDD 2.从外部存储创建RDD 3.从其他RDD转换得到新的RDD 二.从集合中创建RDD 1.使用parallelize函数创建 scala> v ...