题目描述

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. jedis & common pool

    http://mvnrepository.com/artifact/redis.clients/jedis http://mvnrepository.com/artifact/org.apache.c ...

  2. js导航栏单击事件背景颜色变换

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  3. this在java中的用法

    this在java中的用法 1.使用this关键字引用成员变量 作用:解决成员变量与参数或局部变量命名冲突的问题 public class Dog { String name; public Dog( ...

  4. ES6语法 Promise Iterator

    类和对象 基本定义: class Parent{ constructor(name='lmx'){ //name= 默认值 this.name=name } } let v_parent = new ...

  5. Oracle 11g R2性能优化 SQL TRACE

    作为Oracle官方自带的一种基本性能诊断工具,SQL Trace可以用来评估当前正在运行的SQL语句的效率,同时为该语句生成统计信息等,并保存这些信息到指定路径下的跟踪文件(trace)当中.SQL ...

  6. vuex 状态管理

    npm安装:cnpm install --save vuex 安装完:cnpm install main.js引入: import Vuex from 'vuex' Vue.use(Vuex);

  7. 用原生js+canvas实现五子棋

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. Canvas Demo

    <!DOCTYPE html> <html> <head> <title>ゆき</title> </head> <styl ...

  9. windows server 2008 R2服务器无法通过ShellClass获取mp3音乐时长

    我们先看一段代码,获取mp3播放时长: #region GetMediaDetailInfo 获取媒体文件属性信息 /// <summary> /// 获取媒体文件属性信息 /// < ...

  10. IT题库5-并发和并行

    并发和并行从宏观上来讲都是同时处理多路请求的概念.但并发和并行又有区别,并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔内发生.