题目:

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. netflow-module

    https://www.elastic.co/guide/en/logstash/current/netflow-module.html

  2. 【转】GLSL资料收集

    https://blog.csdn.net/u013467442/article/details/44457869 其中入门资料相当好:https://blog.csdn.net/racehorse/ ...

  3. Django学习之路由系统

    一.Django的路由系统 1.URLconf配置 基本格式: 参数说明: 2.正则表达式详解 基本配置 注意事项 补充说明 3.分组命名匹配 URLconf匹配的位置 捕获的参数永远都是字符串 视图 ...

  4. Android studio 不能创建Activity等文件

    这是我之前安装Android studio的一系列问题:http://tieba.baidu.com/p/5921373177 1. 不能创建Activity等许多文件: 2. 工程运行不了: 3. ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第2节 反射_11_反射_案例

    student定义sleep方法 用反射+配置文件 定义配置文件 src下new file.新建 加载配置文件.Properties里面有一个load方法,可以加载.properties结尾的配置文件 ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_06 Set集合_2_哈希值

    没有重写就是十进制的整数,重写了想返回多少就是多少. 创建Person类,没有写继承关系,默认会继承Object类 打开Object这个类 找到HashCode这个方法.就这一行代码.甚至都没有方法体 ...

  7. Centos7上安装Nginx两种方法

    源码编译安装 http://nginx.org/en/download.html 到官网下载,然后用XFTP上传到root目录 把文件解压出来 tar -zxvf nginx-1.16.0.tar.g ...

  8. [USACO 2008 Jan. Silver]架设电话线 —— 最短路+二分

    一道图论的最短路题.一开始连最短路都没想到,可能是做的题太少了吧,完全没有思路. 题目大意: FJ的农场周围分布着N根电话线杆,任意两根电话线杆间都没有电话线相连.一共P对电话线杆间可以拉电话线,第i ...

  9. Common Linux Commands 日常工作常用Linux命令

      How to know CPU info      cat /proc/cpuinfo      arch   How to know memory info: cat /proc/meminfo ...

  10. 事件 on emit off 封装

    /* on 绑定 emit 触发 off 解绑 //存放事件 eventList = { key:val handle:[] } 1对多 on(eventName,callback); handle: ...