LeetCode120-Triangle-数组,动态规划
题目描述
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-数组,动态规划的更多相关文章
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Triangle(动态规划)
题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Leetcode120.Triangle三角形最小路径和
给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和为 11 ...
- LeetCode120 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 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 ...
- POJ - 1163 The Triangle 【动态规划】
一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...
- leetcode 刷题(数组篇)152题 乘积最大子数组 (动态规划)
题目描述 给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积. 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子 ...
- [Scoi2014]方伯伯的玉米田 二维树状数组+动态规划
考试最后半个小时才做这道题.十分钟写了个暴力还写挂了..最后默默输出n.菜鸡一只. 这道题比较好看出来是动规.首先我们要明确一点.因为能拔高长度任意的一段区域,所以如果从i开始拔高,那么一直拔高到n比 ...
随机推荐
- jmeter 之 if controller
jmeter版本5.0.下面是jmeter5.0的if逻辑控制器的截图 标红字体的意思大概是,如果勾选了 下面的 interpret condition as variable expression ...
- Linux下进程和端口常用操作
https://blog.csdn.net/s573626822/article/details/80680456
- 监听器 Listener
监听器:监听某个对象状态的变化 被监听的对象:request.session.servletContext 监听对象的创建和销毁/对象属性的变化 ServletContext HttpSession ...
- js动态改变setInterval的时间
<!DOCTYPE html> <html> <head> <!--meta name="viewport" content=" ...
- Spring Boot 国际化及点击链接跳转国家语言
一.国际化 在SpringBoot中已经自动帮我们配置管理国际化资源的组件,所以我们只需要编写代码就可. @Bean @ConfigurationProperties(prefix = "s ...
- 简单的linux使用命令
1 查看日志异常 tail -f 实时查看日志文件 tail -f 日志文件logtail - 100f 实时查看日志文件 后一百行tail -f -n 100 catalina.out linux查 ...
- RN如何基于js代码手动打一个main.jsbundle
react-native bundle --entry-file index.js --bundle-output ./ios/bundle/main.jsbundle --platform ios ...
- linux服务器情况
查看Linux 进程命令 ps -aux 或者ps -ef linux 进程很多 如果需要查找某一个进程可以使用 管道和grep命令 Linux下常用命令 grep 匹配字符 ps 查询Li ...
- Redis和mysql数据怎么保持数据一致的?
需求起因 在高并发的业务场景下,数据库大多数情况都是用户并发访问最薄弱的环节.所以,就需要使用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问MySQL等数据库. 这个业务场景, ...
- python可以提高程序执行速度N倍你知道吗?
1.1.Numba的约5分钟指南 Numba是Python的即时编译器,它最适用于使用NumPy数组和函数以及循环的代码.使用Numba的最常用方法是通过其装饰器集合,可以应用于您的函数来指示Numb ...