题目描述

Problem Description:

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

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.

分析

    看过刘汝佳的《算法竞赛入门经典》的同学对这道题应该都不陌生,因为这是那本书讲动规里面举的第一个案例,可能也是很多人第一次接触动规时候的启蒙题目。

    对于这种问题维度较低,且无需寻径的求最优解问题,直接推出递推方程:\(M(i,j) = min(M(i+1,j),M(i+1,j+1)) + v(i,j)\),然后在题目给出的数据上实现递推方程的搜索过程即可。

    一般这种问题都有自底向上和自顶向下两种递推式,上述的递推式是自底向上的形式。另外一种懒得想了。

解决方案

  1. //Solution
  2. class Solution120 {
  3. public int minimumTotal(List<List<Integer>> triangle) {
  4. for(int i=triangle.size()-2; i>=0; i--) {
  5. List<Integer> nc = triangle.get(i);
  6. List<Integer> lc = triangle.get(i+1);
  7. for(int j=0; j<nc.size(); j++) {
  8. nc.set(j, nc.get(j)+(lc.get(j)<lc.get(j+1)?lc.get(j):lc.get(j+1)));
  9. }
  10. }
  11. return triangle.get(0).get(0);
  12. }
  13. }

LeetCode120-Triangle-数组,动态规划的更多相关文章

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

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

  2. Triangle(动态规划)

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

  3. HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)

    Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  5. Leetcode120.Triangle三角形最小路径和

    给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和为 11 ...

  6. LeetCode120 Triangle

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

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

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

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

  9. leetcode 刷题(数组篇)152题 乘积最大子数组 (动态规划)

    题目描述 给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积. 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子 ...

  10. [Scoi2014]方伯伯的玉米田 二维树状数组+动态规划

    考试最后半个小时才做这道题.十分钟写了个暴力还写挂了..最后默默输出n.菜鸡一只. 这道题比较好看出来是动规.首先我们要明确一点.因为能拔高长度任意的一段区域,所以如果从i开始拔高,那么一直拔高到n比 ...

随机推荐

  1. css阴影框

    选中div浮动的阴影框.example-card:hover {box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);transform: translate3d(0, ...

  2. 关于映射路径@ReuqestMapping的总结

    何谓映射路径呢? 映射路径,就是匹配请求路径和执行方法关系的路径 基于注解的映射路径可以忽略前后缀,如: @RequestMapping(value="/say.do") @Req ...

  3. supervisor无法监听uwsgi

    ➜ ~ sudo supervisorctl data_config FATAL Exited too quickly (process log may have details) data_conf ...

  4. ubuntu安装zabbix 3.2(转)

    转自:http://www.zabbix.org.cn/viewtopic.php?f=13&t=1096本人略做了写修改. 准备工作 apt-get update apt-get upgra ...

  5. CocosCreator的ToggleGroup组件使用

    用了CocosCreator也有一段时间,对ToggleGroup始终没有仔细的学习过,只停留在用过的水平.所以因为认识有限,所以以为ToggleGroup对自定义支持得没那么好,这两天因为项目,再学 ...

  6. 获取上一行记录lag

    SELECT EMPLID ,EFFDT ,END_DT ,COMPANY ,DEPTID ,POSITION_NBR ,' ' ,' ' FROM ( SELECT J1.EMPLID ,J1.EF ...

  7. SSH整合时多表关联查询出现Javassist增强失败

    Customer类对应的表为另一个表LinkMan的外键,在进行LinkMan表操作时,出现如下错误. 遇到Javassist增强失败网上说法不一,有的说Customer没有无参构造方法,javass ...

  8. C# 同步更新网盘和本地的文件夹及文件

    该程序是可以更新本地文件或更新网盘文件或者网盘和本地同步更新 下载地址:https://files.cnblogs.com/files/Wonderful-Life/UpdateFilesSync.r ...

  9. 堆叠式降噪自动编码器(SDA)

    1.1 自动编码器  自动编码器(AutoEncoder,AE)就是一种尽可能复现输入信号的神经网络,其输出向量与输入向量同维,常按照输入向量的某种形式,通过隐层学习一个数据的表示或对原始数据进行有效 ...

  10. 笔记本电脑没有Pause键,远程桌面无法全屏

    用过mstsc远程桌面的都知道,可以用CTRL+ALT+Break 切换为全屏操作,但有些品牌的电脑不知道设计理念是啥,居然没有Break键,解决办法就是用Fn+B键替换Break键,也就是同时按住C ...