题目:

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的更多相关文章

  1. 九章算法系列(#4 Dynamic Programming)-课堂笔记

    前言 时隔这么久才发了这篇早在三周前就应该发出来的课堂笔记,由于懒癌犯了,加上各种原因,实在是应该反思.好多课堂上老师说的重要的东西可能细节上有一些急记不住了,但是幸好做了一些笔记,还能够让自己回想起 ...

  2. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  3. 【动态规划】The Triangle

    问题 E: [动态规划]The Triangle 时间限制: 1 Sec  内存限制: 128 MB提交: 24  解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...

  4. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

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

  6. POJ - 1163 The Triangle 【动态规划】

    一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...

  7. LeetCode -- Triangle 路径求最小和( 动态规划问题)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. Triangle(动态规划)

    题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  9. LeetCode之“动态规划”:Triangle

    题目链接 题目要求: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...

随机推荐

  1. Nginx与Lua开发

    1.Lua及基础语法 Nginx与Lua环境 场景:用Nginx结合Lua实现代码的灰度发布 1.Lua 是一个简洁.轻量.可扩展的脚本语言 2.Nginx+Lua优势 充分的结合Nginx的并发处理 ...

  2. String、StringBuffer与StringBuilder介绍

    关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.三者在执行速度方面的比较:     StringBuilder > St ...

  3. linux 搭建环境

    报错:cannot find valid baseurl for repo:base 解决办法: https://blog.csdn.net/banqgg/article/details/782560 ...

  4. 在命令行运行java代码

    因为尝试将运行结果通过管道命令保存,所以尝试在命令行(不借助lde来运行java代码,结果折腾了半天) 仿照的是eclipse创建文件目录的方式 最终解决方法是: #/bin/bash root_di ...

  5. Spring Boot 集成 JPA 的步骤

    Spring Boot 集成 JPA 的步骤 配置依赖 compile group: 'org.springframework.boot', name: 'spring-boot-starter-da ...

  6. 关于Java泛型实现原理的思考与一般用法示例总结

    面向对象的一个重要目标是对代码重用的支持.支持这个目标的一个重要机制就是泛型机制.在1.5版本之前,java并没有直接支持泛型实现,泛型编程的实现时通过使用继承的一些基本概念来完成的. 这种方式的局限 ...

  7. centos 6.9 mysql 安装配置

    1.全新系统,安装mysql yum -y install mysql mysql-server mysql-devel 2.启动mysql service mysqld start 3.修改密码 登 ...

  8. springboot定时任务出错 Unexpected use of scheduler.

    java.lang.IllegalStateException: Unexpected use of scheduler. 在启动类加: @Bean public ThreadPoolTaskSche ...

  9. vscode-常用插件介绍(10大插件)

    https://www.cnblogs.com/zhaoshujie/p/9834654.html 本文介绍了目前前端开发最受欢迎的开发工具 VSCode 必装的 10 个开发插件,用于大大提高软件开 ...

  10. 【原创】基于phpGrace+uniApp开发之:5.登录界面增加图片验证码

    1.目的: 采用phpGrace中的图片验证码,在用户名+密码登录时使用图片验证码进行验证. 2.文档地址: 图片验证码的文档地址:http://www.phpgrace.com/tools/info ...