题目描述

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)\),然后在题目给出的数据上实现递推方程的搜索过程即可。

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

解决方案

//Solution
class Solution120 {
public int minimumTotal(List<List<Integer>> triangle) {
for(int i=triangle.size()-2; i>=0; i--) {
List<Integer> nc = triangle.get(i);
List<Integer> lc = triangle.get(i+1);
for(int j=0; j<nc.size(); j++) {
nc.set(j, nc.get(j)+(lc.get(j)<lc.get(j+1)?lc.get(j):lc.get(j+1)));
}
}
return triangle.get(0).get(0);
}
}

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. Mybatis+Mysql逆向工程

    目录结构: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  2. weblogic8控制台禁止(允许)访问配置方法

    由于现网上对外网开放,而weblogic控制台的信息和管理比较敏感,如果weblogic控制台被破解账户和密码登录, 面临的风险将是非常的大,所以一般现网部署时,要禁用掉weblogic控制台的访问. ...

  3. CF1142C U2

    题目链接:洛谷 codeforces $y>x^2+bx+c$也就是$y-x^2>bx+c$ 左边是点,右边是直线. 维护上凸包. 虽然这么简单但就是做不出来. #include<c ...

  4. html5网页录音

    demo https://xiangyuecn.github.io/Recorder/

  5. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列一:

    从库报这个错误:Got fatal error 1236 from master when reading data from binary log: 'Could not find first lo ...

  6. 【git】强制覆盖本地代码(与git远程仓库保持一致)

    git强制覆盖:    git fetch --all    git reset --hard origin/master    git pull git强制覆盖本地命令(单条执行):    git ...

  7. 浅析对spring中IOC的理解

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  8. MongoDB系列----查

    开启查询: db.getMongo().setSlaveOk() 查版本: db.servion(); db.serverBuildInfo(); db.serverStatus().storageE ...

  9. Javascript扩展String.prototype实现格式金额、格式时间、字符串连接、计算长度、是否包含、日期计算等功能

    <script src="Js/jquery-3.1.1.min.js"></script> <script type="text/java ...

  10. Vue2leaflet 替换国内地图api,带{z}/{x}/{y}形式的

    参考:https://www.cnblogs.com/gispathfinder/p/9535685.html Vue2leaflet安装后,默认自带的地图URL如下 url:'http://{s}. ...